activeMQ_helloworld(一)

一、activeMQ下载,直接在Linux上wget http://mirror.bit.edu.cn/apache//activemq/5.14.5/apache-activemq-5.14.5-bin.tar.gz

使用tar -zxvf 解压即可,启动activeMQ很简单,直接cd到bin目录,./activemq start即可

activeMQ的默认端口是61616,后台管理界面的端口是8161,如果你的防火墙拦截了这些端口,你需要打开这些端口或者是关闭防火墙,

vim /etc/sysconfig/iptables

修改后需要让修改生效,键入/etc/init.d/iptables restart这条命令即可

2、打开管理界面http://192.168.243.128:8161/admin,输入用户名admin,密码admin,可以看到以下界面

二、生产者代码

 1 package com.aciveMQ;
 2 
 3 import javax.jms.Connection;
 4 import javax.jms.ConnectionFactory;
 5 import javax.jms.JMSException;
 6 import javax.jms.MessageProducer;
 7 import javax.jms.Queue;
 8 import javax.jms.Session;
 9 import javax.jms.TextMessage;
10 
11 import org.apache.activemq.ActiveMQConnectionFactory;
12 
13 public class Producer {
14     
15     private static final String BROKER_URL = "tcp://192.168.243.128:61616";
16     
17     public static void main(String[] args) {
18         ConnectionFactory connectionFactory;// 连接工厂
19         Connection connection = null;// 连接
20         Session session = null;// 会话
21         Queue destination;// 目标
22         MessageProducer messageProducer;// 消息生产者
23 
24         connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
25 
26         try {
27             connection = connectionFactory.createConnection();
28             
29             // 第一个参数表示是否加事物操作,第二个参数Session.AUTO_ACKNOWLEDGE表示自动确认接收,
30             //
31             session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
32             destination = session.createQueue("myFirstQueue");// 创建一个叫做myFirstQueue的目标队列
33             
34             messageProducer = session.createProducer(destination);// 创建消息生产者
35             TextMessage tm = session.createTextMessage("hello world");
36             
37             connection.start();
38             
39             messageProducer.send(tm);
40             session.commit();// 启动了事物就必须提交,否则不能发消息
41         } catch (JMSException e) {
42             e.printStackTrace();
43         } finally {
44             if (connection != null) {
45                 try {
46                     session.close();
47                     connection.close();
48                 } catch (JMSException e) {
49                     e.printStackTrace();
50                 }
51             }
52 
53         }
54     }
55     
56 
57 }
2、消费者

 1 package com.aciveMQ;
 2 
 3 import javax.jms.Connection;
 4 import javax.jms.ConnectionFactory;
 5 import javax.jms.JMSException;
 6 import javax.jms.MessageConsumer;
 7 import javax.jms.Queue;
 8 import javax.jms.Session;
 9 import javax.jms.TextMessage;
10 
11 import org.apache.activemq.ActiveMQConnectionFactory;
12 
13 /**
14  * @author may
15  *
16  */
17 public class Consumer {
18     private static final String BROKER_URL = "tcp://192.168.243.128:61616";
19 
20     public static void main(String[] args) {
21 
22         ConnectionFactory connectionFactory = null;// 连接工厂
23         Connection connection = null;// 连接
24         Session session = null;// 会话
25         Queue destination;// 目标
26         MessageConsumer messageConsumer;
27 
28         try {
29             connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
30             connection = connectionFactory.createConnection();
31             // 第一个参数表示是否加事物操作,第二个参数Session.AUTO_ACKNOWLEDGE表示自动确认接收,
32             // 消费消息不需要加事物
33             session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
34             destination = session.createQueue("myFirstQueue");// 创建一个叫做myFirstQueue的目标主题
35             messageConsumer = session.createConsumer(destination);
36 
37             connection.start();
38 
39             // receive(long argue)在取到队列中的消息后,会按每1s钟的时间再次读取
40             TextMessage textMessage = (TextMessage) messageConsumer.receive(1000);
41             if (textMessage != null) {
42 
43                 System.out.println(textMessage.getText());
44 
45             }
46             
47  48             
49             // System.out.println(textMessage);
50 
51         } catch (JMSException e) {
52             e.printStackTrace();
53         } finally {
54             if (connection != null) {
55                 try {
56                     session.close();
57                     connection.close();
58                 } catch (JMSException e) {
59                     e.printStackTrace();
60                 }
61             }
62 
63         }
64 
65     }
66 
67 }

3、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.aciveMQ</groupId>
    <artifactId>activeMQ_hello</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>activeMQ_hello</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.14.5</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>


三、测试
启动生产者,然后再启动消费者,就会输出hello world。


原文地址:https://www.cnblogs.com/honger/p/7009059.html