`

IBM MQ example:

阅读更多
2006-09-08

IBM MQ example:


package com.bulain.wasmq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQQueueConnectionFactory;

public class CreateMq implements MessageListener {
	private String qName = "clq_default_mphch085";
	private String HOSTNAME = "10.240.13.81";
	private String QMGRNAME = "QM_mphch085";
	private String CHANNEL = "S_mphch085";
	private Integer PORTINT = new Integer(1414);
	private String CLIENTID = "";
	private Integer CCSID = new Integer(932);
	private String username="mqadmin";
	private String password="";
	
	private QueueConnection qConnection = null;
	private QueueSession qSession = null;
	private Queue ioQueue;
	private QueueSender sender;
	private QueueReceiver receiver;	

	public static void main(String[] args) throws JMSException {
		CreateMq mq = new CreateMq();
		mq.testMQ();
	}
	
	public void testMQ() throws JMSException{
		makeQueueConnection();
		createSender();
		sendMessage("This is a test");
		createReceiver();		
		//disconnect();
	}

	public void makeQueueConnection() {
		try {
			// Create a Queue Connection Factory & set its properties
			MQQueueConnectionFactory factory = new MQQueueConnectionFactory();
			factory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
			factory.setQueueManager(QMGRNAME);
			factory.setHostName(HOSTNAME);
			factory.setChannel(CHANNEL);
			factory.setPort(PORTINT.intValue());

			if (CCSID != null && CCSID.intValue() > 0) {				
				factory.setCCSID(CCSID.intValue());
			}

			// Create a Connection
			qConnection = factory.createQueueConnection(username,password);

			// The client identifier is ignored for point to point connections. Since
			// we are using point to point connections, there is no need to set the ClientID.
			qConnection.setClientID( CLIENTID );
			qConnection.start();

			// Create a Session
			boolean transacted = false;
			qSession = qConnection.createQueueSession(transacted, Session.AUTO_ACKNOWLEDGE);

			// Create a Queue
			String QUEUENAME = qName;
			// String queueParam = "queue://" + QMGRNAME + "/" + QUEUENAME + "?targetClient=1";
			// AIUJP : ADD THE CCSID in the queue paramater
			// queueParam = "queue://" + QMGRNAME + "/" + QUEUENAME + "?targetClient=1&CCSID="+CCSID.intValue();
			String queueParam = buildQueueParam(QMGRNAME, QUEUENAME);
			ioQueue = qSession.createQueue(queueParam);
		} catch (Exception e) {
		}
	}
	
	private String buildQueueParam(String qMgr, String qName) {
		String queueParam = null;
		if (qMgr == null || (qMgr != null && qMgr.trim().length() <= 0)) {
			return queueParam;
		}

		if (qName == null || (qName != null && qName.trim().length() <= 0)) {
			return queueParam;
		}

		queueParam = "queue://" + qMgr + "/" + qName + "?targetClient=1";
		if (CCSID != null && CCSID.intValue() != 0) {			
			queueParam += "&CCSID=" + CCSID.intValue();
		}

		return queueParam;
	}
	
	public void createReceiver() throws JMSException{
		receiver = qSession.createReceiver(ioQueue);
		receiver.setMessageListener(this);		
		qConnection.start();
	}
	
	public void createSender() throws JMSException{
		sender = qSession.createSender(ioQueue);
	}
	public void sendMessage(String message) throws JMSException{
		Message msg = qSession.createTextMessage(message);
		sender.send(msg);
	}
	
	public void onMessage(Message message) {
		try {
			TextMessage tMessage = (TextMessage) message;
			String text;
			text = tMessage.getText();
			System.out.println("Received message <" + text + "> with ID <" + message.getJMSMessageID() + ">");
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	public void disconnect() throws JMSException {
		receiver.close();
		qSession.close();
		qConnection.stop();
		qConnection.close();
		System.out.println("Receiver stopped.");
	}
}


分享到:
评论

相关推荐

    IBM MQ 8.0 Jar包

    public class MQExample { public static void main(String[] args) { try { int openOptions = CMQC.MQOO_INPUT_AS_Q_DEF | CMQC.MQOO_OUTPUT; MQQueueManager qMgr = new MQQueueManager("QMGR_NAME"); MQ...

    C# 实现消息的收发IBM WebSphere MQ 消息队列

    public class MQExample { public static void Main(string[] args) { // 从appconfig获取配置 string queueManager = ConfigurationManager.AppSettings["QueueManager"]; string host = ConfigurationManager...

    IBM WebSphere MQ 第二章

    ### IBM WebSphere MQ 第二章知识点详解 #### 2章:WebSphere MQ 的构成要素 在第一章中,我们概述了WebSphere MQ的基本概念及其在消息传递系统中的作用。本章节将深入探讨WebSphere MQ的核心组成部分——队列和...

    WebsphereMQ查看软件

    public class MQExample { public static void main(String[] args) throws Exception { // 创建MQConnectionFactory实例 MQConnectionFactory factory = new MQConnectionFactory(); // 设置队列管理器名称 ...

    MQ java编程

    在本文中,我们将深入探讨如何使用Java编程与IBM MQ(Message Queuing)进行交互,特别是如何实现客户端A作为消息发送方的功能。IBM MQ是一种企业级的消息中间件,它允许应用程序通过队列进行异步通信,确保消息的...

    jms-activemq-example:使用Apache ActiveMQ的Belajar JMS

    提供者,即面向消息的中间件或充当代理的应用程序,示例之一是Apache ActiveMQ,RabitMQ,Hive MQTT,IBM MQ,JBoss Messaging等。 客户端,它是充当消息的发送者或接收者的应用程序。 生产者/发布者,它是充当...

    MQ使用文档

    MQSeries(现在称为IBM MQ)是一款成熟的企业级消息传递系统,旨在为应用程序之间的通信提供可靠的基础。MQSeries支持多种编程流模式,每种模式都有其独特的应用场景。本文档将详细介绍MQSeries中的几种常见编程流...

    原IBM架构师梁岳传博士撰写的MQ技术文档,经典

    ### 运行示例 (Running the example) 文档还提到了运行示例的步骤,包括直接连接设置和间接连接设置。这些步骤对于确保MQ环境正确配置至关重要。 #### 直接连接设置 直接连接设置主要包括以下步骤: 1. **定义...

    MQjava基础编程.pdf

    WebSphere MQ,通常简称为MQ,是IBM提供的一种消息中间件,用于在分布式系统中可靠地传输消息。在Java环境中,MQ提供了与Java消息服务(JMS)接口的集成,使得Java开发者能够方便地发送和接收消息。本文档将探讨MQ ...

    Spring + JMS + MQ Connection

    在IT行业中,Spring框架...在实际项目中,可以根据MQ的具体实现(如ActiveMQ、RabbitMQ或IBM WebSphere MQ)调整ConnectionFactory的相关属性。在压缩包中的`Demo`可能包含了相关的代码示例,可以结合阅读以加深理解。

Global site tag (gtag.js) - Google Analytics