`
huanglz19871030
  • 浏览: 241797 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

ActiveMq例子代码

    博客分类:
  • JMS
阅读更多
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQPrefetchPolicy;
import org.apache.camel.component.jms.JmsMessage;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;

//发送TextMessage
public class SendMessage {
    
    private static final String url = "tcp://localhost:61616";;
    private static final String QUEUE_NAME = "choice.queue";
    protected String expectedBody = "<hello>world!</hello>";
    
    public void sendMessage() throws JMSException{

        Connection connection = null;
        
        try{            
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
            connection = connectionFactory.createConnection();
            
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);          
            Destination destination = session.createQueue(QUEUE_NAME);
            MessageProducer producer = session.createProducer(destination);
            TextMessage message = session.createTextMessage(expectedBody);
            message.setStringProperty("headname", "remoteB");
                producer.send(message);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            connection.close();
        }
    }


***************************************************************************************
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
//发送BytesMessage
public class SendMessage {
    
    private String url = "tcp://localhost:61616";
        
    public void sendMessage() throws JMSException{
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session  session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("test.queue");
        MessageProducer producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        BytesMessage message = session.createBytesMessage();
        byte[] content = getFileByte("d://test.jar");
        message.writeBytes(content);
        try{
            producer.send(message);
            System.out.println("successful send message");
        }catch(Exception e){
            e.printStackTrace();
            e.getMessage();
        }finally{
            session.close();
            connection.close();
        }                
    }
    
    private byte[] getFileByte(String filename){
        byte[] buffer = null;
        FileInputStream fin = null;
        try {
            File file = new File(filename);
            fin = new FileInputStream(file); 
            buffer = new byte[fin.available()];
            fin.read(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return buffer;
    }
发送完消息后可以访问 
http://localhost:8161/admin/queues.jsp 
看到相应的queue中是否有消息 

 

 

 

 

 适用收取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();
    }
}
0
0
分享到:
评论

相关推荐

    ActiveMQ例子

    本压缩文件包含了apache-activemq-5.8.0-bin.tar.gz安装文件和一个详细的ActiveMQ实例,欢迎下载

    apache-activemq-5.0.0-src.zip_ActiveMQ 源代码_activemq_activemq.src

    ActiveMQ源代码,包括开发想到和开发例子。

    activemq入门实例,有源代码

    activemq入门代码,里面有详细的操作步骤,是入门的不错例子,包含基本消息应用方

    Spring boot 和内置ActiveMQ集成例子.zip

    Spring boot 和内置ActiveMQ集成例子,通过代码学习Spring boot 和内置ActiveMQ集成。

    ActiveMQ C++ Windows客户端 3.8.3例子代码(含头文件和编译出来的dll和lib)

    activemq的最新的CMS客户端代码是3.8.3,由于在windows下编译比较麻烦,遍寻网络不获,所以自己仔细研究了一下,终于编译成功,分享给大家。附件中的例子工程在vs2010下编译通过。如果需要移植到自己的工程里,仅...

    activemq-demo

    activemq demo 例子代码 ,启动 activemq 直接本地可以运行

    JMS之ActiveMQ工具类+使用例子.zip

    Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件;由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行。

    ActiveMQ 5.7源码及jar包

    apache-activemq 5.7 源码及jar包 含帮助文档,源代码,例子等。项目可部署运行,亲测。

    activemq资源包

    来自apache官网的activemq源码包,有所有jar包,有demo,还有发送和接收消息的代码例子(在word文档里面)。完整的activemq开发包。

    activemqDemo

    activemqDemo,里面有简单的代码实现,通过代码例子能够让小伙伴们更好的学习

    ActiveMQ,Spring的简单例子

    http://blog.csdn.net/live501837145/article/details/46532159 代码实例

    JMS+activeMQ消息中间件

    最全的基于spring mvc的JMS+activeMQ实现的消息中间件代码例子,源程序和apache-activemq-5.10.0-bin.zip

    terraform-aws-activemq:提供ActiveMQ的基本模块

    在您现有的Terraform代码中将此模块的module.activemq.tf包含为模块: module " activemq " { source = " JamesWoolfenden/activemq/aws " version = " v0.1.1 " common_tags = var . common_tags subnet_ids ...

    企业消息总线(ESB)源代码

    ActiveMQ 企业消息总线(ESB)源代码 本代码包含ESB在实际项目中的应用例子。注意测试前请下载安装 apache-activemq-5.12.1

    JAVA上百实例源码以及开源项目源代码

     Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...

    active MQ ,消息队列

    activeMQ的一个小例子 包含处理附件的代码例子 完整版 包含jar包和配置文件

    spring mq集成 web工程发送和接收消息例子

    spring mq集成 web工程发送和接收消息例子 亲自测试可以用 代码机构清晰 需要自行安装mq服务端

    JAVA上百实例源码以及开源项目

     Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...

    黑马品优购项目

    4.2. 举几个简单模块的例子 4.2.1. 品牌管理 单表 分页、新增、删除、修改 4.2.2. 规格管理 2张表 分页、新增、删除、修改、显示优化(显示列表内容的一部分) 4.2.3. 模板管理 2张表 分页、新增、删除、修改、...

Global site tag (gtag.js) - Google Analytics