前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JMS的常用方法

JMS的常用方法

作者头像
用户3003813
发布2018-09-06 13:21:40
6960
发布2018-09-06 13:21:40
举报
文章被收录于专栏:个人分享
代码语言:javascript
复制
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 org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import com.ailk.biapp.ci.localization.cntv.service.IUserSynchronizationService;
import com.asiainfo.biframe.utils.config.Configure;
import com.asiainfo.biframe.utils.spring.SystemServiceLocator;

public class JmsTopicReceiver {
    
    public void topicListener() {

        // ConnectionFactory :连接工厂,JMS 用它创建连接
        ConnectionFactory connectionFactory;
        // Connection :JMS 客户端到JMS Provider 的连接
        Connection connection = null;
        // Session: 一个发送或接收消息的线程
        Session session;
        // Destination :消息的目的地;消息发送给谁.
        Destination destination;
        // 消费者,消息接收者
        MessageConsumer consumer;
        
        String activeUrl = Configure.getInstance().getProperty("ACTIVE_URL");
        String topic = Configure.getInstance().getProperty("TOPIC_NAME");
        
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD, activeUrl);
        try {
            
            // 构造从工厂得到连接对象
            connection = connectionFactory.createConnection();
            // 启动
            connection.start();
            // 获取操作连接
            session = connection.createSession(Boolean.FALSE,
                    Session.AUTO_ACKNOWLEDGE);
            //test-queue跟sender的保持一致,一个创建一个来接收
            destination = session.createTopic(topic);
            consumer = session.createConsumer(destination);
            consumer.setMessageListener(new MessageListener() {
                public void onMessage(Message message) {
                    try {
                        TextMessage txtMsg = (TextMessage)message;
                        String msg = txtMsg.getText();
                        //收到topic的时候增量同步用户
                        IUserSynchronizationService service = 
                                (IUserSynchronizationService)SystemServiceLocator.getInstance().getService("userSynchronizationServiceImpl");
                        service.startIncrementalSync(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            });

       } catch (Exception e) {
            e.printStackTrace();
        }

    }
    
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-11-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档