JMS&MQ系列之简单示例

        1. 发送Message

/**
 * 
 * @author geloin
 * @date 2012-9-10 下午6:22:43
 */
package com.geloin.activemq.test1;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * 
 * @author geloin
 * @date 2012-9-10 下午6:22:43
 */
public class MyMessageProducer {
	public static void main(String[] args) throws Exception {
		ConnectionFactory factory = null;
		Connection conn = null;
		Session session = null;
		Destination dest = null;
		MessageProducer producer = null;
		Message message = null;
		boolean useTransaction = false;
		try {
			Context ctx = new InitialContext();
			factory = (ConnectionFactory)ctx.lookup("ConnectionFactoryName");
			conn = factory.createConnection();
			conn.start();
			session = conn.createSession(useTransaction, Session.AUTO_ACKNOWLEDGE);
			dest = session.createQueue("TEST.QUEUE");
			producer = session.createProducer(dest);
			message = session.createTextMessage("This is a test");
			producer.send(message);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			producer.close();
			session.close();
			conn.close();
		}
		
		
	}
}

        2. 同步接收消息

/**
 * 
 * @author geloin
 * @date 2012-9-11 上午9:45:53
 */
package com.geloin.activemq.test1;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * 
 * @author geloin
 * @date 2012-9-11 上午9:45:53
 */
public class MySyncMessageConsumer {

	/**
	 * 
	 * @author geloin
	 * @date 2012-9-11 上午9:45:54
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		ConnectionFactory factory = null;
		Connection conn = null;
		Session session = null;
		Destination dest = null;
		MessageConsumer consumer = null;
		Message message = null;
		boolean useTransaction = false;
		try {
			Context ctx = new InitialContext();
			factory = (ConnectionFactory) ctx.lookup("ConnectionFactoryName");
			conn = factory.createConnection();
			conn.start();
			session = conn.createSession(useTransaction, Session.AUTO_ACKNOWLEDGE);
			dest = session.createQueue("TEST.QUEUE");
			consumer = session.createConsumer(dest);
			message = (TextMessage)consumer.receive(1000);
			System.out.println("Received message: " + message);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			consumer.close();
			session.close();
			conn.close();
		}
	}

}

        3. 异步接收消息

/**
 * 
 * @author geloin
 * @date 2012-9-11 下午1:12:40
 */
package com.geloin.activemq.test1;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * 
 * @author geloin
 * @date 2012-9-11 下午1:12:40
 */
public class MyAsyncMessageConsumer implements MessageListener {

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
	 */
	@Override
	public void onMessage(Message message) {
		if (message instanceof TextMessage) {
			System.out.println("Received message:" + message);
		}
	}

	/**
	 * 
	 * 
	 * @author geloin
	 * @date 2012-9-11 下午1:13:33
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		ConnectionFactory factory = null;
		Connection conn = null;
		Session session = null;
		Destination dest = null;
		MessageConsumer consumer = null;
		boolean useTransaction = false;
		try {
			Context ctx = new InitialContext();
			factory = (ConnectionFactory) ctx.lookup("ConnectionFactoryName");
			conn = factory.createConnection();
			conn.start();
			session = conn.createSession(useTransaction,
					Session.AUTO_ACKNOWLEDGE);
			dest = session.createQueue("TEST.QUEUE");
			consumer = session.createConsumer(dest);
			consumer.setMessageListener(new MyAsyncMessageConsumer());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			consumer.close();
			session.close();
			conn.close();
		}
	}
}


你可能感兴趣的:(JMS&MQ系列之简单示例)