JMS 简单实例

使用 ActiveMQ 当做 JMS 提供者,写一个简单的 demo。

主要是模拟一个聊天室的样子,下面是源代码

package com.mycompany.app;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/** * Hello world! * */
public class App implements MessageListener { 
   
    private TopicSession pubSession;    
    
    private TopicPublisher publisher;   
     
    private TopicConnection connection;    
    
    private String username;    
    
    private static String user = ActiveMQConnection.DEFAULT_USER;    
    
    private static String password = ActiveMQConnection.DEFAULT_PASSWORD;    
    
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;    
    
    public App(String username) throws Exception {        
        this.username = username;//聊天室的用户名
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(user, password, url);
        //创建ActiveMQ提供的链接工厂
        connection = factory.createTopicConnection();
        pubSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);        
        TopicSession subSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);           Topic topic = subSession.createTopic("myTopic");
        publisher = pubSession.createPublisher(topic);        
        TopicSubscriber subscriber = subSession.createSubscriber(topic, null, true);
        subscriber.setMessageListener(this);
        connection.start();
    }    
    
    public static void main(String[] args) throws Exception {        
        App app = new App(args[0]);        
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));        
        while (true) {            
            String line = reader.readLine();            
            if ("exit".equals(line)) {
                app.close();
            } else {
                app.writeMessage(line);
            }
        }
    }    
    
    public void onMessage(Message msg) {        
        TextMessage message = (TextMessage) msg;        
        try {            
            System.out.println(message.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }    
    
    public void writeMessage(String text) throws Exception {        
        TextMessage message = pubSession.createTextMessage();
        message.setText(username + " : " + text);
        publisher.publish(message);
    }    public void close() throws Exception {
        connection.close();
    }
}


安装 ActiveMQ

mac 安装比较简单 brew install activemq

ActiveMQ 会被默认安装到 /usr/local/Cellar/activemq

先运行 activemq setup ~/.activemqrc 来指定 activemq 的环境配置文件。

运行 activemq start 可以在一个独立进程中启动 activemq

$ activemq start
INFO: Loading '/usr/local/Cellar/activemq/5.11.1/libexec/bin/env'
INFO: Using java '/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details
INFO: pidfile created : '/usr/local/Cellar/activemq/5.11.1/libexec/data/activemq.pid' (pid '4880')


终止 ActiveMQ 的运行有两种方式。一种是使用 activemq stop。

另一种则是暴力的杀死进程,即 kill 4880。

运行

通过 maven 启动 3个 main 方法:

mvn exec:java -Dexec.mainClass="com.mycompany.app.App" -Dexec.args="rcx1"
mvn exec:java -Dexec.mainClass="com.mycompany.app.App" -Dexec.args="rcx2"
mvn exec:java -Dexec.mainClass="com.mycompany.app.App" -Dexec.args="rcx3"

然后在其中一个里面说一句话,其他都会收到信息。

// rcx2
rcx1 : hello
hello two
rcx3 : 大家好

// rcx1
hello
rcx2 : hello two
rcx3 : 大家好

// rcx3
大家好
rcx1 : hello
rcx2 : hello two

【参考资料】 java消息服

转载于:https://my.oschina.net/biezhi/blog/490771

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值