JMS(Java Messaging Service)基础

1.基础结构

1)图表结构

 2)各部分介绍

ConnectionFactory

封装了一组连接配置参数,它已被定义为管理员。每个连接工厂都是队列(Queue)连接工厂或主题(Topic)连接工厂接口的一个实例

连接工厂的创建

private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

关于DEFULT_BROKER_URL这个常量的来源,通过查找源码,可以得到

 所以和tcp://localhost:61616得到的结果一样

注:

activeMQ默认配置下启动会启动8161和61616两个端口,其中8161是mq自带的管理后台的端口,61616是mq服务默认端口 。

8161是后台管理系统,61616是给java用的tcp端口。

原文:(107条消息) activeMQ的两个默认端口8161和61616的区别_blueSkyGoGo的博客-CSDN博客_activemq默认端口号

用如下方式同样可以获取连接:

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

Connection

使用连接来创建一个或多个会话

它有两种形式,即队列(Queue)连接或主题(Topic)连接

你可以用对应的连接工厂进行创建连接

QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();

建立连接后执行start()方法。在使用完毕后执行close()

Destination

客户端用于指定其生成的消息的目标和它所使用的消息的源的对象

Session

会话是用于生成和使用消息的单个线程上下文

会话的创建

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

第一个参数:是否支持事务,如果为true,则会忽略第二个参数,被jms服务器设置为SESSION_TRANSACTED
第一个参数为false时,第二个参数的值可为

Seesion.AUTO_ACKNOWLEDGE, //AUTO_ACKNOWLEDGE 为自动确认,客户端发送和接收消息不需要做额外的工作

Session.CLIENT_ACKNOWLEDGE,//CLIENT_ACKNOWLEDGE为客户端确认,客户端接收到消息后,必须调用javax.jms,Messagede1acknow方法

Session.DUPS_OK_ACKNOWLEDGE//DUPS_OK_ACKNOWLEDGE允许副本的确认模式,一旦接收方应用程序的方法调用从处理消息处返回,会话对象就会确认

您可以使用会话来创建消息生产者、消息使用者和消息。

MessageProducer producer = session.createProducer(queue);
MessageConsumer consumer = session.createConsumer(queue);

3)消息message

消息类型:

TextMessage: 一个java.lang。字符串对象(例如,可扩展标记语言文件的内容)
Object message : javax.jms.ObjectMessage,表示一个JAVA对象。
BytesMessage : javax.jms.BytesMessage,表示字节数据。
Stream message :javax.jms.StreamMessage,表示java原始值数据流。
MapMessage :一组名称/值对,其中名称作为字符串对象,值作为Java编程语言中的原始类型。

消息的发送:

Queue queue = session.createQueue(subject);
MessageProducer producer = session.createProducer(queue);
TextMessage message1 = session.createTextMessage("message1");
producer.send(message1);

消息的接收:

Message m = queueReceiver.receive();
if (m instanceof TextMessage) {
TextMessage message = (TextMessage)m;
Systems.out.println(“Reading message: “ + message.getText();
} else {
// Handle error
}

2.代码实现

1)发送方(producer,sender)

package com.activemq.sender;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessageSender {
    //URL of the JMS server. The default broker url is the localhost.
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

    // a. Complete the queue name
    private static String subject = "111";

    public static void main(String[] args) throws JMSException{
        // Getting JMS connection from the server
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        // b. Complete the start connection
        Connection connection = connectionFactory.createConnection();
        connection.start();
        // c. Create the session
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(subject);
        //Destination. The queue will be created automatically on the server
        MessageProducer producer = session.createProducer(queue);
        MessageProducer producer2 = session.createProducer(session.createQueue("queue"));
        TextMessage message1 = session.createTextMessage("111");
        producer.send(message1);
        producer2.send(message1);
        producer.close();
        session.close();
        connection.close();
    }
}

2)接收方(consumer,receiver)

package com.activemq.receiver;
import javax.jms.*;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessageReceiver {
    //URL of the JMS server. The default broker url is the localhost.
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

    // a. Complete the queue name
    private static String subject = "111";

    public static void main(String[] args) throws JMSException {
        // Getting JMS connection from the server
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        // b. Complete the start connection
        Connection connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue(subject);
        MessageConsumer consumer = session.createConsumer(destination);
        /*while (true){
            TextMessage textMessage = (TextMessage) consumer.receive();
            if (textMessage!=null){
                String text = textMessage.getText();
                System.out.println("The message received is "+text);
            }else {
                System.out.println("Message not received");
                break;
            }
        }*/
            if (consumer.receive() instanceof TextMessage textMessage) {
            System.out.println("Received message '" + textMessage.getText() + "'");
            consumer.close();
            session.close();
            connection.close();
            // c. Create the session

            //Destination. The queue will be created automatically on the server.
        }
    }
}

3.ActiveMQ查看

在安装目录下/bin/win64文件中启动命令窗口,输入activemq启动jms服务器

在浏览器中输入localhost:8161,输入账号密码进入管理页面

默认账号密码:admin/admin

点击进入管理activemq代理 

 查看队列

每次执行sender程序,消息会进入队列 ,并加入待处理队列中

每次执行receiver程序,消息出队,待处理数减一

每次关闭activemq服务器,会清空“进入队列数”,但消息仍然会存在队列中,下一次启动后仍能出队

部分来源:(107条消息) idea下的activemq的简单demo_小白摸爬滚打之路的博客-CSDN博客_idea连接activemq

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值