ActiveMq发送接收对象

当前配置是允许接收所有的对象序列化,更多设置详情参考官网:
http://activemq.apache.org/objectmessage.html

1、保证当前MQ能正常接收发送消息
2、定义一个实体类Member继承Serializable

package com.activemq.test;

import java.io.Serializable;
import java.util.Date;

@SuppressWarnings("serial")
public class Member implements Serializable{
    private String name;
    private double score;
    private int age;
    private Date brithday;
    public Member(){}
    public Member(String name, double score, int age, Date brithday) {
        super();
        this.name = name;
        this.score = score;
        this.age = age;
        this.brithday = brithday;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getBrithday() {
        return brithday;
    }
    public void setBrithday(Date brithday) {
        this.brithday = brithday;
    }
    @Override
    public String toString() {
        return "Member [name=" + name + ", score=" + score + ", age=" + age + ", brithday=" + brithday + "]";
    }

}

4、发送端测试代码

package com.activemq.test;

import java.util.Date;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class TestSendMq {

    private static final int SEND_NUMBER = 5;



    /**
     * @param args
     */
    public static void main(String[] args) {

        // ConnectionFactory:连接工厂,JMS用它创建连接

        ConnectionFactory connectionFactory;

        // Connection:JMS客户端到JMS Provider的连接

        Connection connection = null;

        // Session:一个发送或接收消息的线程

        Session session;

        // Destination:消息的目的地;消息发送给谁.

        Destination destination;

        // MessageProducer:消息发送者

        MessageProducer producer;

        // TextMessage message;

        //构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar

        connectionFactory = new ActiveMQConnectionFactory(

                ActiveMQConnection.DEFAULT_USER,

                ActiveMQConnection.DEFAULT_PASSWORD,

                "failover:(tcp://0.0.0.0:61618)");

        try {

            //构造从工厂得到连接对象

            connection =connectionFactory.createConnection();

            //启动

            connection.start();

            //获取操作连接

            session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

            //获取session 

            destination = session.createQueue("FirstQueue");

            //得到消息生成者【发送者】

            producer =session.createProducer(destination);

            //设置不持久化,此处学习,实际根据项目决定

           producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            //构造消息,此处写死,项目就是参数,或者方法获取

            sendMessage(session, producer);

            session.commit();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                if (null != connection)

                    connection.close();

            } catch (Throwable ignore) {

            }

        }

        }



    public static void sendMessage(Session session,MessageProducer producer)

            throws Exception {

        for (int i = 1; i <=SEND_NUMBER; i++) {
            Member a = new Member("测试账号"+i,11.1,15+i,new Date());
//          HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
//          map.put(i, i);
            ObjectMessage message = session

                    .createObjectMessage(a);

            //发送消息到目的地方

            System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);

            producer.send(message);

        }

    }

}

3、接收端测试代码

package com.activemq.test;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class TestNewCounsumerMq {

    public static void main(String[] args) {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://0.0.0.0:61618");
//      factory.setTrustedPackages(new ArrayList<String>(Arrays.asList("com.activemq.test")));
        factory.setTrustAllPackages(true);  //允许接收所有的数据~~~
        org.apache.camel.component.jms.JmsConfiguration conf = new  org.apache.camel.component.jms.JmsConfiguration();
        conf.setConnectionFactory(factory);
        org.apache.activemq.camel.component.ActiveMQComponent comp= new org.apache.activemq.camel.component.ActiveMQComponent();
        comp.setConfiguration(conf);
        Connection connection = null ;
        try {
            connection = factory.createConnection();
             //启动

            connection.start();

            //获取操作连接

            Session session = connection.createSession(false,

                    Session.AUTO_ACKNOWLEDGE);

            //获取session

            Destination destination = session.createQueue("FirstQueue");

            MessageConsumer consumer =session.createConsumer(destination);
            System.out.println(consumer);
            while (true) {

                //设置接收者接收消息的时间,为了便于测试,这里谁定为100s

                ObjectMessage message =(ObjectMessage) consumer.receive(100000);
                if (null != message) {
                    Member a = (Member)message.getObject();
                    System.out.println("收到消息" +a.getName());

                    } else {

                        break;

                    }

                }
        } catch (JMSException e) {
            e.printStackTrace();
        }finally{
            try {

                if (null != connection)

                connection.close();

        } catch (Throwable ignore) {

        }
        }

    }

}

备注:以上纯属测试,更多安全详情配置请参考官网
http://activemq.apache.org/objectmessage.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值