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="Art515940";
	
	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,jms)