activemq实现简单的消息传递(java 实现)


activemq实现简单的消息传递,

消息传递有两种消息处理模式:点对点(PTP) 和订阅/发布(sub/pub)
 PTP的目地地是QUEUE,  sub/pub的目的地是TOPIC

Topic和queue的最大区别在于topic是以广播的形式,通知所有在线监听的客户端有新的消息,没有监听的客户端将收不到消息;而queue则是以点对点的形式通知多个处于监听状态的客户端中的一个。

下面实现消息生产者代码(sub/pub)

package com.panther.dong.activemq.pubsub.consumer;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jms.*;

/**
 * Created by panther on 15-8-18.
 */
public class ConsumerMessage {
    private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerMessage.class);
    private static final String url = "tcp://192.168.236.150:61616?connectionTimeout=0&wireFormat.maxInactivityDuration=0";

    private Session session = null;

    private Connection connection = null;

    private MessageConsumer messageConsumer = null;

    private static ConsumerMessage instance = new ConsumerMessage();

    public static ConsumerMessage getInstance() {
        return instance;
    }

    public ConsumerMessage() {
        this.init();
    }

    public void init() {
        try {
            ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(url);
            if (activeMQConnectionFactory != null) {
                connection = (Connection) activeMQConnectionFactory.createConnection();
                connection.start();
            }
            if (connection != null) {
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                Destination destination = session.createTopic("panther.test");
                messageConsumer = session.createConsumer(destination);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getMessage() {
        String result = null;
        try {
            Message message = messageConsumer.receive(1000);
            if (message instanceof TextMessage) {
                result = ((TextMessage) message).getText();
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        for (int i = 0; i < Integer.MAX_VALUE; ++i) {
            String result = ConsumerMessage.getInstance().getMessage();
            System.out.println("----+++--------" + result);
        }
    }
}
消息消费者代码(pub/sub)
package com.panther.dong.activemq.pubsub.consumer;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jms.*;

/**
 * Created by panther on 15-8-18.
 */
public class ConsumerMessage {
    private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerMessage.class);
    private static final String url = "tcp://192.168.236.150:61616?connectionTimeout=0&wireFormat.maxInactivityDuration=0";

    private Session session = null;

    private Connection connection = null;

    private MessageConsumer messageConsumer = null;

    private static ConsumerMessage instance = new ConsumerMessage();

    public static ConsumerMessage getInstance() {
        return instance;
    }

    public ConsumerMessage() {
        this.init();
    }

    public void init() {
        try {
            ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(url);
            if (activeMQConnectionFactory != null) {
                connection = (Connection) activeMQConnectionFactory.createConnection();
                connection.start();
            }
            if (connection != null) {
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                Destination destination = session.createTopic("panther.test");
                messageConsumer = session.createConsumer(destination);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getMessage() {
        String result = null;
        try {
            Message message = messageConsumer.receive(1000);
            if (message instanceof TextMessage) {
                result = ((TextMessage) message).getText();
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        for (int i = 0; i < Integer.MAX_VALUE; ++i) {
            String result = ConsumerMessage.getInstance().getMessage();
            System.out.println("----+++--------" + result);
        }
    }
}


运行结果:

生产者生产消息,消费者消费消息




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值