ActiveMQ(三)——理解和掌握JMS

发布时间:2020-06-26 17:27:37 作者:mazongfei
来源:网络 阅读:863

一、JMS基本概念

二、 jms的消息结构

三、JMS的可靠性机制

四、JMS的PTP模型

五、JMS的Pub/Sub模型

七、JMS应用开发的基本步骤
ActiveMQ(三)——理解和掌握JMS

八、非持久的Topic消息示例

3:由于不知道客户端发送多少信息,因此改成while循环的方式了,例如:

Message message = consumer.receive();
while(message!=null){
TextMessage txtMsg= (TextMessage)message;
System.out.println(“收到消息:”+txtMsg.getText());
message = consumer.receive(1000L);
}
public class NoPersistenceReceive {
    public static void main(String[] args) throws Exception{
        //连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = connectionFactory.createConnection();
        connection.start();

        //带事务的session
        final Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);

        Destination destination = session.createTopic("myTopic");
        MessageConsumer consumer = session.createConsumer(destination);

        Message message = consumer.receive();
        while(message!=null){
            TextMessage txtMsg= (TextMessage)message;
            System.out.println("收到消息:"+txtMsg.getText());
            message = consumer.receive(1000L);
        }
        session.commit();
        session.close();
        connection.close();
    }
}

注意:对于非持久性的Topic消息,则需要接收者保持运行状态,不然消息发送者发出的消息会接收不到。
ActiveMQ(三)——理解和掌握JMS

九、持久的Topic消息示例
1:需要在连接上设置消费者ID,用来识别消费者
2:需要创建TopicSubscriber来订阅
3:要设置好了过后再start这个connnection
4:消费者一定要先运行一次,等于向消息服务中间件注册这个消费者,然后再运行客户端发送消息,这个时候无论消费者是否在线,都会接收到,下次连接的时候,会把没有收过的消息都接收下来。

public class PersistenceSender {
    public static void main(String[] args) throws Exception{
        //连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = connectionFactory.createConnection();

        //带事务的session
        Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createTopic("myTopic");
        MessageProducer producer = session.createProducer(destination);

        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        connection.start();

        for (int i = 0; i < 3; i++) {
            TextMessage message = session.createTextMessage("message---"+i);
            producer.send(message);
        }
        session.commit();
        session.close();
        connection.close();
    }
}
public class PersistenceReceive {
    public static void main(String[] args) throws Exception{
        //连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = connectionFactory.createConnection();
        connection.setClientID("cc1");

        //带事务的session
        final Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);

        Topic destination = session.createTopic("myTopic");
        TopicSubscriber ts = session.createDurableSubscriber(destination,"T1");
        connection.start();

        Message message = ts.receive();
        while(message!=null){
            TextMessage txtMsg= (TextMessage)message;
            System.out.println("收到消息:"+txtMsg.getText());
            message = ts.receive(1000L);
        }
        session.commit();
        session.close();
        connection.close();
    }
}

ActiveMQ(三)——理解和掌握JMS

十、总结

推荐阅读:
  1. JMS与ActiveMQ消息数据持久化
  2. ActiveMQ(二)——ActiveMQ的安装和基本使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

activemq 中间件 ct

上一篇:java判断字符串是否包含指定字符的方法

下一篇:java和c语言有什么不同

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》