dedecms织梦内容管理系统    
首页 | java | C/C++ | PHP | 操作系统 | ajax | 脚本编程 | 安全技术 | 本站下载页 | flex | CRM | 专题 | QQ群 | 测试中心 | 会员中心 | 积分规则
  当前位置:主页>java>开源框架>文章内容
收取消息的activemq的例子
来源: 作者:
 适用收取TextMessage消息 
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;


public class ReceiveMessage {
    
    private static final String url = "tcp://172.16.168.167:61616";
    private static final String QUEUE_NAME = "szf.queue";

    
    public void receiveMessage(){
        Connection connection = null;
        try{
            try{
                ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
                connection = connectionFactory.createConnection();
            }catch(Exception e){
//                ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
//                connection = connectionFactory.createConnection();
            }            
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(QUEUE_NAME);
            MessageConsumer consumer = session.createConsumer(destination);
            consumeMessagesAndClose(connection,session,consumer);
        }catch(Exception e){
            
        }
    }
    
    protected void consumeMessagesAndClose(Connection connection,
            Session session, MessageConsumer consumer) throws JMSException {
        for (int i = 0; i < 1;) {
            Message message = consumer.receive(1000);
            if (message != null) {
                i++;
                onMessage(message);
            }
        }
        System.out.println("Closing connection");
        consumer.close();
        session.close();
        connection.close();
    }
    
    public void onMessage(Message message){
        try{
            if (message instanceof TextMessage) {
                TextMessage txtMsg = (TextMessage)message;
                String msg = txtMsg.getText();
                System.out.println("Received: " + msg);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
        
    public static void main(String args[]){
        ReceiveMessage rm = new ReceiveMessage();
        rm.receiveMessage();
    }
}

上一篇:activemq例子代码   下一篇:flex文件上传显示进度条
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·关于JSF和Struts的讨论
·Struts教程-Struts模块化编程教
·Struts入门经验
·用科学的思维方法指导软件的设计
·Hibernate配置文件中映射元素详
·Spring中事件处理的小技巧
·struts2.0pring2.0 hibernate3.2
·struts2.0 spring2.0 hibernate3
·浅谈hibernate lazy fetch
·Hibernate的Fetch
·优化hibernate性能的几点建议
·Hibernate中的取策略延迟加载
  相关文章
·activemq例子代码
·hibernate登录例子代码
·hibernate"null id in entry (do
·JFreeChart教程 -- 入门
·JFreeChart初学者入门实例详细解
·使用JUnit对EJB进行单元测试
·响应JSF的请求 事件
·JSF中的事件
·JSF与Struts的比较
·JSF1.2技术的新特性总结
·简介JSF
·JSF错误讯息处理
  相关信息
copy right @ 百家拳软件项目研究室 2007 辽ICP备07011763