JMS实战之四 ------JMS学习环境的搭建

JMS学习环境的搭建

1、  打开eclipse,新建项目”learnJMS”

2、  添加JMS相关包,由于我们使用的消息中间件为ActiveMQ,所以需要从ACTIVEMQ_HOME\lib目录下的相关包:

3、创建三个类,Publisher.java,Subscriber.java,Listener.java代码如下:

 

package ex01;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.InitialContext;

public class Publisher {

 private TopicConnection connection;
 private TopicPublisher publisher;
 private TopicSession session;
 
 public Publisher() throws Exception
 {
  InitialContext init=new InitialContext();
  TopicConnectionFactory topicConnectionFactory=(TopicConnectionFactory)init.lookup("TopicCF");
  connection=topicConnectionFactory.createTopicConnection();
  session=connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
  Topic topic=(Topic)init.lookup("topic1");
  publisher=session.createPublisher(topic);
  
  connection.start();
 }
 
 public void writeMessage(String message) throws JMSException
 {
  TextMessage textMessage=session.createTextMessage();
  textMessage.setText(message);
  publisher.publish(textMessage);
 }
 
 public void close() throws JMSException
 {
  connection.close();
 }
 
 public static void main(String[] args)
 {
  try {
   Publisher publisher = new Publisher();
   publisher.writeMessage("nihao!");
//   publisher.close();
  } catch (Exception e) {
   e.printStackTrace();
  }  
  
 }
}

 

 

 

package ex01;

import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.InitialContext;

public class Subscriber {

 private TopicConnection connection;
 private TopicSession session;
 private TopicSubscriber subscriber;
 
 public Subscriber() throws Exception
 {
  InitialContext init=new InitialContext();
  TopicConnectionFactory connectionFactory=(TopicConnectionFactory)init.lookup("TopicCF");
  connection=connectionFactory.createTopicConnection();
  session=connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
  Topic topic=(Topic)init.lookup("topic1");
  subscriber=session.createSubscriber(topic);
  
  subscriber.setMessageListener(new Listener());
  connection.start();
 }
 
 public void close() throws JMSException
 {
  connection.close();
 }
 
 public static void main(String[] args) {
  
  try {
   Subscriber sub=new Subscriber();
//   sub.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

 

 

 

package ex01;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class Listener implements MessageListener {

 public void onMessage(Message message) {
  
  try {
   TextMessage textMessage = (TextMessage) message;
   System.out.println("message is :"+textMessage.getText());
  } catch (Exception e) {
   e.printStackTrace();
  }  
 }

}

 

4、启动ActiveMQ,然后运行Subscriber.java。Publisher.java类

可以看到Subscriber收到了Publisher发送的信息:nihao。

 

JMS学习环境搭建成功。下一节我们学习JMSAPI。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

登峰大数据

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值