Home  >  Article  >  Java  >  Create a simple Hello World case

Create a simple Hello World case

PHP中文网
PHP中文网Original
2017-06-21 17:11:471740browse

1. Create a simple Hello World case

First you need to import the activemq-all-5.14.5.jar package and write the production side:

package com.ietree.mq.helloworld;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.DeliveryMode;import javax.jms.Destination;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class Sender {public static void main(String[] args) throws Exception {// 第一步:建立ConnectionFactory工厂对象,需要填入用户名、密码、以及要连接的地址,均使用默认即可,默认端口为:tcp://localhost:61616ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");// 第二步:通过ConnectionFactory工厂对象我们创建一个Connection链接,并且调用ConnectionFactory的start方法开启链接,Connection默认是关闭的Connection connection = connectionFactory.createConnection();
        connection.start();// 第三步:通过Connection对象创建Session会话(上下文环境对象),用于接收消息,参数配置1为是否启用事务,参数配置2为签收模式,一般我们设置为自动签收Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);// 第四步:通过Session创建Destination对象,指的是一个客户端用来指定生产消息目标和消费信息来源的对象,在PTP模式中,Destination被称作Queue即队列;在Pub/Sub模式,Destination被称作Topic即主题。在程序中可以使用多个Queue和Topic。Destination destination = session.createQueue("queue1");// 第五步:我们需要通过Session对象创建消息的发送和接收对象(生产者和消费者)MessageProducer/MessageConsumer。MessageProducer messageProducer = session.createProducer(destination);// 第六步:我们可以使用MessageProducer的setDeliveryMode方法为其设置持久化特性和非持久化特性(DeliveryMode)        messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);// 第七步:最后我们使用JMS规范的TextMessage形式创建数据(通过Session对象),并用MessageProducer的send方法发送数据。同理,客户端使用receive方法进行接收数据,最后需要关闭Connection连接。for (int i = 0; i < 5; i++) {
            TextMessage textMessage = session.createTextMessage();
            textMessage.setText("我是消息内容......" + i);
            messageProducer.send(textMessage);
            System.out.println("生产者:" + textMessage.getText());
        }if (connection != null) {
            connection.close();
        }
    }
}

Write Consumer side:

package com.ietree.mq.helloworld;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class Receiver {public static void main(String[] args) throws Exception {// 第一步:建立ConnectionFactory工厂对象,需要填入用户名、密码、以及要连接的地址,均使用默认即可,默认端口为:tcp://localhost:61616ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");// 第二步:通过ConnectionFactory工厂对象我们创建一个Connection链接,并且调用ConnectionFactory的start方法开启链接,Connection默认是关闭的Connection connection = connectionFactory.createConnection();
        connection.start();// 第三步:通过Connection对象创建Session会话(上下文环境对象),用于接收消息,参数配置1为是否启用事务,参数配置2为签收模式,一般我们设置为自动签收Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);// 第四步:通过Session创建Destination对象,指的是一个客户端用来指定生产消息目标和消费信息来源的对象,在PTP模式中,Destination被称作Queue即队列;在Pub/Sub模式,Destination被称作Topic即主题。在程序中可以使用多个Queue和Topic。Destination destination = session.createQueue("queue1");// 第五步:我们需要通过Session对象创建消息的发送和接收对象(生产者和消费者)MessageProducer/MessageConsumer。MessageConsumer messageConsumer = session.createConsumer(destination);        while (true) {
            TextMessage msg = (TextMessage) messageConsumer.receive();if(msg == null){break;
            }
            System.out.println("接收到的内容:" + msg.getText());
        }if (connection != null) {
            connection.close();
        }
    }
}

Check the message consumption through the http://localhost:8161/admin/queues.jsp path.

2. ActiveMQ security mechanism

activeMQ’s Web management interface: http://127.0.0.1:8161/admin
The activeMQ management console is deployed using jetty, so you need to change the password. Go to the corresponding configuration file: apache-activemq-5.14.5\conf\jetty-realm.properties
activeMQ should be set up with a security mechanism. Only users who meet the authentication can send and obtain messages, so we can also use activemq. Add security verification configuration to xml, apache-activemq-5.14.5\conf\jetty-realm.properties\activemq.xml, just add a plug-in configuration after line 123.

Add the administrative user in activemq.xml:

<!-- destroy the spring context on shutdown to stop jetty --><shutdownHooks><bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" /></shutdownHooks><plugins>
            <simpleAuthenticationPlugin>
                <users>
                    <authenticationUser username="root" password="root" groups="users,admins"/>
                </users>
            </simpleAuthenticationPlugin>
        </plugins></broker>

In this way, you can only connect through the root account information.

The above is the detailed content of Create a simple Hello World case. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn