[原创]JMS两种消息类型的相关代码_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2665 | 回复: 0   主题: [原创]JMS两种消息类型的相关代码        下一篇 
lijun.fang
注册用户
等级:上尉
经验:751
发帖:29
精华:0
注册:1970-1-1
状态:离线
发送短消息息给lijun.fang 加好友    发送短消息息给lijun.fang 发消息
发表于: IP:您无权察看 2015-4-3 12:10:49 | [全部帖] [楼主帖] 楼主

JMS两种消息类型的相关代码(可以直接复制粘贴运行)

准备介质:myeclipse10+weblogic12c
项目截图:

北京联动北方科技有限公司
jndi.properties的内容:

java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.provider.url=t3\://127.0.0.1\:7001


相关代码如下(仅供测试,代码没有问题):
①、Queue类型:

SendMessage.java
package com.landingbj.queue;
import java.util.Properties;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
public class SendMessage {
      public static void main(String[] args) {
            QueueSender sender;// 消息的发送者
            TextMessage msg = null;// 消息
            try {
                  Properties properties = new Properties();
                  Context context = new InitialContext();
                  // 通过广场获得链接,通过链接获得会话
                  QueueConnectionFactory queueFactory = (QueueConnectionFactory) context
                  .lookup("ConnectionFactory-jms");
                  QueueConnection qConnection = queueFactory.createQueueConnection();
                  QueueSession qsession = qConnection.createQueueSession(false,
                  Session.AUTO_ACKNOWLEDGE);
                  // 获得消息队列Queue-jms为自己绑定的JNDI名称
                  Queue que = (Queue) context.lookup("Queue-jms");
                  // 建立目的地和消息
                  sender = qsession.createSender(que);
                  msg = qsession.createTextMessage();
                  // 消息内容
                  msg.setText("aaaddjcijd22jhsiodj");
                  sender.send(msg);
                  System.out.println("successful!");
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
}
ReceiveMessage.java
package com.landingbj.queue;
import java.util.Properties;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class ReceiveMessage {
      public static void main(String[] args) throws JMSException {
            QueueReceiver receiver;
            TextMessage msg;
            try {
                  Properties properties=new Properties();
                  Context ctx = new InitialContext();
                  QueueConnectionFactory qcf = (QueueConnectionFactory) ctx
                  .lookup("ConnectionFactory-jms");
                  QueueConnection qc = qcf.createQueueConnection();
                  QueueSession qsession = qc.createQueueSession(false,
                  Session.AUTO_ACKNOWLEDGE);
                  qc.start();
                  Queue que = (Queue) ctx.lookup("Queue-jms");
                  receiver = qsession.createReceiver(que);
                  msg = (TextMessage) receiver.receive();
                  System.out.println("你发的消息是..." + msg.getText());
            } catch (NamingException e) {
                  e.printStackTrace();
            } catch (JMSException e) {
                  e.printStackTrace();
            }
      }
}


②、Topic类型

TopicProducer.java
package com.landingbj.topic;
import java.util.Properties;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.TopicConnectionFactory;
public class TopicProducer {
      public static void main(String[] args) {
            TextMessage msg = null;// 消息
            try {
                  Properties properties=new Properties();
                  Context context = new InitialContext();
                  // 通过工厂获得连接,通过连接获得会话
                  TopicConnectionFactory topicFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory-jms");
                  TopicConnection topConnection = topicFactory.createTopicConnection();
                  TopicSession session = topConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
                  topConnection.start();
                  Topic topic = (Topic) context.lookup("Topic-jms");
                  TopicPublisher publisher = session.createPublisher(topic);
                  msg = session.createTextMessage();
                  msg.setText("我的public model!");// 消息内容
                  publisher.publish(msg);
                  System.out.println("successful!");
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
}
TopicReceives.java
package com.landingbj.topic;
import java.util.Properties;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
public class TopicReceives {
      public static void main(String[] args) {
            try {
                  Properties properties=new Properties();
                  Context context = new InitialContext();
                  TopicConnectionFactory factory = (TopicConnectionFactory) context.lookup("ConnectionFactory-jms");
                  TopicConnection conn = factory.createTopicConnection();
                  TopicSession session = conn.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
                  Topic messageTopic = (Topic) context.lookup("Topic-jms");
                  TopicSubscriber subscriber = session.createSubscriber(messageTopic);
                  conn.start();
                  Message msg = subscriber.receive();
                  String msgText = ((TextMessage)msg).getText();
                  Thread.sleep(2000);
                  System.out.println("jun--" + msgText);
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
}
TopicReceives2.java
package com.landingbj.topic;
import java.util.Properties;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
public class TopicReceives2 {
      public static void main(String[] args) {
            try {
                  Properties properties=new Properties();
                  Context context = new InitialContext();
                  TopicConnectionFactory factory = (TopicConnectionFactory) context.lookup("ConnectionFactory-jms");
                  TopicConnection conn = factory.createTopicConnection();
                  TopicSession session = conn.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
                  Topic messageTopic = (Topic) context.lookup("Topic-jms");
                  TopicSubscriber subscriber = session.createSubscriber(messageTopic);
                  conn.start();
                  Message msg = subscriber.receive();
                  String msgText = ((TextMessage)msg).getText();
                  System.out.println("TopicReceives2--" + msgText);
                  Thread.sleep(4000);
                  System.out.println("aaa");
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
}


相关概念请参看:

JMS 类型及程序设计模型:http://bbs.landingbj.com/showtopic.jsp?boardcode=WLTL&hit=11&showid=253773&rootid=253773

Weblogic知识要点之JMS配置:http://bbs.landingbj.com/showtopic.jsp?boardcode=ZJJ&hit=4&showid=250143&rootid=250143

JMS点对点实例:http://bbs.landingbj.com/showtopic.jsp?rootid=245801&CPages=1

JMS 发布与订阅:http://bbs.landingbj.com/showtopic.jsp?rootid=245887&CPages=1

JMS队列收发消息实例:http://bbs.landingbj.com/showtopic.jsp?boardcode=WLTL&hit=356&showid=248071&rootid=248071



该贴被lijun.fang编辑于2015-4-3 12:12:59



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论