学习笔记——JMS示例

来源:互联网 发布:windows10怎么切换mac 编辑:程序博客网 时间:2024/06/06 02:04
 

发送消息示例:

import javax.jms.*;import javax.naming.*;import java.util.Properties;public class MesssageSender {public void sendMessage() throws NamingException, JMSException {//定义WebLogic默认连接工厂的JNDIfinal String CONNECTION_FACTORY_JNDI = "weblogic.jms.ConnectionFactory";//获取JNDI服务所需的ContextContext context = getInitialContext();//通过JNDI查找获取连接工厂ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup(CONNECTION_FACTORY_JNDI); //通过JNDI查找获取消息目的Destination destination = (Destination)context.lookup("MessageQueue");//连接工厂创建连接Connection connection = connectionFactory.createConnection();//JMS连接创建JMS会话Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//JMS会话创建消息生产者MessageProducer sender = session.createProducer(destination);//设置消息生产者生产出来的消息的传递模式、有效时间。sender.setDeliveryMode(DeliveryMode.PERSISTENT);sender.setTimeToLive(20000);//通过JMS会话创建一个文本消息TextMessage message = session.createTextMessage();//message.setStringProperty("ConType","txt");message.setText("Hello");sender.send(message);session.close();connection.close();}private Context getInitialContext() {final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory";final String SERVER_URL = "t3://localhost:7001";Context context = null;try {Properties properties = new Properties();properties.put(Context.INITIAL_CONTEXT_FACTORY, INIT_FACTORY);properties.put(Context.PROVIDER_URL , SERVER_URL);context = new InitialContext(properties);}catch (NamingException exception) {System.err.println("不能连接WebLogic Server在:" + SERVER_URL);ne.printStackTrace();}}public static void main(String[] args) {MessageSender messageSender = new MessageSender();messageSender.sendMessage();}}


消息同步接收示例:

 

import javax.jms.*;import javax.naming.*;import java.util.Properties;public class SyncConsumer { public void receiveMessage()throws JMSException , NamingException {//定义WebLogic默认连接工厂的JNDIfinal String CONNECTION_FACTORY_JNDI = "weblogic.jms.ConnectionFactory";//获取JNDI服务所需的ContextContext context = getInitialContext();//通过JNDI查找获取连接工厂ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup(CONNECTION_FACTORY_JNDI);//通过JNDI查找获取消息目的Destination destination = (Destination)context.lookup("MessageQueue");//连接工厂创建连接Connection connection = connectionFactory.createConnection();//启动JMS连接,让它开始传输JMS消息connection.start();//JMS连接创建JMS会话Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//JMS会话创建消息消费者MessageConsumer receiver = session.createConsumer(destination);//同步接收消息,如果没有接收到消息,该方法会阻塞线程TextMessage message = (TextMessage)receiver.receive();System.out.println(message);System.out.println("同步接收到的消息:" + message.getText());//关闭资源session.close();connection.close();}//工具方法,用来获取命名服务的Context对象private Context getInitialContext() {final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory";final String SERVER_URL = "t3://localhost:7001";Context context = null;try {Properties properties = new Properties();properties.put(Context.INITIAL_CONTEXT_FACTORY , INIT_FACTORY);properties.put(Context.PROVIDER_URL , SERVER_URL);context = new InitialContext(properties);}catch(NamingException ne) {System.err.println("不能连接WebLogic Server在:"+ SERVER_URL);ne.printStackTrace();}return context;}public static void main(String[] args)throws Exception {SyncConsumer mp = new SyncConsumer();mp.receiveMessage();}}

 

消息异步接受示例:

 

import javax.jms.*;import javax.naming.*;import java.util.Properties;public class AsyncConsumerimplements MessageListener {public AsyncConsumer()throws NamingException,JMSException , InterruptedException {//定义WebLogic默认连接工厂的JNDIfinal String CONNECTION_FACTORY_JNDI = "weblogic.jms.ConnectionFactory";//获取JNDI服务所需的ContextContext context = getInitialContext();//通过JNDI查找获取连接工厂ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup(CONNECTION_FACTORY_JNDI); //通过JNDI查找获取消息目的Destination destination = (Destination)context.lookup("MessageQueue");//连接工厂创建连接Connection connection = connectionFactory.createConnection();//启动JMS连接,让它开始传输JMS消息connection.start();//JMS连接创建JMS会话Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//JMS会话创建消息消费者MessageConsumer receiver = session.createConsumer(destination);//为JMS消息消费者绑定消息监听器receiver.setMessageListener(this);//程序暂停20s,在此期间内以异步方式接收消息Thread.sleep(20000);//关闭资源session.close();connection.close();}//实现消息监听器必须实现的方法。public void onMessage(Message m) {TextMessage message = (TextMessage)m;System.out.println(message);try {System.out.println("异步接收的消息:" + message.getText());}catch (JMSException ex) {ex.printStackTrace();}}//工具方法,用来获取命名服务的Context对象private Context getInitialContext() {final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory";final String SERVER_URL = "t3://localhost:7001";Context context = null;try {Properties properties = new Properties();properties.put(Context.INITIAL_CONTEXT_FACTORY , INIT_FACTORY);properties.put(Context.PROVIDER_URL , SERVER_URL);context = new InitialContext(properties);}catch(NamingException ne) {System.err.println("不能连接WebLogic Server在:" + SERVER_URL);ne.printStackTrace();}return context;}public static void main(String[] args)throws Exception {AsyncConsumer consumer = new AsyncConsumer();}}