消息队列ActiveMQ(一)——Queue方式和Topic方式

下载地址
http://activemq.apache.org/download.html,这里下载的版本是5.13.0

windows下安装
直接解压缩,运行apache-activemq-5.13.0\bin\win64\wrapper.exe文件,32位的运行win32目录下该文件

目录说明

   +bin (windows下面的bat和unix/linux下面的sh)
   +conf (activeMQ配置目录,包含最基本的activeMQ配置文件)
   +data (默认是空的)
   +docs (只有index.html)
   +example (几个例子)
   +lib (activemMQ使用到的lib)
   +webapps(后台管理页面)
+webapps-demo(后台管理消息发送页面)
   +activemq-all-5.8.0.jar (java开发的jar包)
   -LICENSE.txt
   -NOTICE.txt
   -README.txt

后台管理登录地址
http://localhost:8161/admin/

初始用户名、密码
admin/admin
用户名密码文件位于:apache-activemq-5.13.0\conf\jetty-realm.properties
值得注意的是 用户名和密码的格式是
用户名 : 密码 ,角色名

点对点(Point-to-Point)方式
jar包:


<dependency>
<groupId>javax.jmsgroupId>
<artifactId>jmsartifactId>
<version>1.1version>
dependency>
<dependency>
    <groupId>org.apache.activemqgroupId>
    <artifactId>activemq-coreartifactId>
    <version>5.5.0version>
dependency>
<dependency>
    <groupId>org.apache.activemqgroupId>
    <artifactId>activemq-poolartifactId>
    <version>5.7.0version>
dependency>


<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>slf4j-apiartifactId>
    <version>1.6.1version>
dependency>
<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>slf4j-log4j12artifactId>
    <version>1.6.1version>
dependency>
<dependency>
    <groupId>log4jgroupId>
    <artifactId>log4jartifactId>
    <version>1.2.16version>
dependency>

生产者:

import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class QueueProducer {

    private static String user = ActiveMQConnection.DEFAULT_USER;  
    private static String password =ActiveMQConnection.DEFAULT_PASSWORD;  
    private static String url =  "tcp://localhost:61616";  

    public static void main(String[] args)throws Exception {  
        // ConnectionFactory :连接工厂,JMS 用它创建连接  
       ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user,password,url);  
       // Connection :JMS 客户端到JMS Provider 的连接  
       Connection connection = connectionFactory.createConnection();  
       // Connection 启动  
       connection.start();  
       System.out.println("Connection is start...");  
       // Session: 一个发送或接收消息的线程  
       Session session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);  
       // Queue :消息的目的地;消息发送给谁.  
       Queue  destination = session.createQueue("example.A");  
       // MessageProducer:消息发送者  
       MessageProducer producer = session.createProducer(destination);  
       // 设置不持久化,此处学习,实际根据项目决定  
       producer.setDeliveryMode(DeliveryMode.PERSISTENT);  
        // 构造消息,此处写死,项目就是参数,或者方法获取  
       sendMessage(session, producer);  
       session.commit();  

       connection.close();  
       System.out.println("send text ok.");  
   }  

   public static void sendMessage(Session session, MessageProducer producer)  
           throws Exception {  
       for (int i = 1; i <= 100; i++) {//有限制,达到1000就不行  
           TextMessage message = session.createTextMessage("ActiveMq 发送的消息" + i);  
           // 发送消息到目的地方  
           System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);  
           producer.send(message);  
       }  
   }  

}

消费者:

import javax.jms.Connection;  
import javax.jms.ConnectionFactory;  
import javax.jms.Destination;  
import javax.jms.JMSException;  
import javax.jms.Message;  
import javax.jms.MessageConsumer;  
import javax.jms.MessageListener;  
import javax.jms.Queue;  
import javax.jms.Session;  
import javax.jms.TextMessage;  

import org.apache.activemq.ActiveMQConnection;  
import org.apache.activemq.ActiveMQConnectionFactory;  

public class QueueConsumer {

    private static String user = ActiveMQConnection.DEFAULT_USER;  
    private static String password =ActiveMQConnection.DEFAULT_PASSWORD;  
    private static String url = "tcp://localhost:61616";  
    public static void main(String[] args) throws Exception{  
        // ConnectionFactory :连接工厂,JMS 用它创建连接  
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user,password,url);  
        // Connection :JMS 客户端到JMS Provider 的连接  
        Connection connection = connectionFactory.createConnection();  
        connection.start();  
        // Session: 一个发送或接收消息的线程  
        final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);  
        // Destination :消息的目的地;消息发送给谁.  
        Queue destination=session.createQueue("example.A");  
        // 消费者,消息接收者  
        MessageConsumer consumer = session.createConsumer(destination);  
        consumer.setMessageListener(new MessageListener(){//有事务限制  
            @Override  
            public void onMessage(Message message) {  
                try {  
                    TextMessage textMessage=(TextMessage)message;  
                    System.out.println(textMessage.getText());  
                } catch (JMSException e1) {  
                    e1.printStackTrace();
                }  
                try {  
                    session.commit();  
                } catch (JMSException e) {  
                    e.printStackTrace();  
                }  
            }  
        });  

/*  另外一种接受方式 
 *    while (true) { 
              //设置接收者接收消息的时间,为了便于测试,这里谁定为100s 
              TextMessage message = (TextMessage) consumer.receive(100000); 
              if (null != message) { 
                  System.out.println("收到消息" + message.getText()); 
              } else { 
                  break; 
              } 
          }*/  
    } 

}

Topic(发布/订阅)方式
发布者

import java.util.Date;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

//Topic(发布/订阅)方式  发布者Publisher  
public class TopicPublisher {
    private static String user = ActiveMQConnection.DEFAULT_USER;
    private static String password = ActiveMQConnection.DEFAULT_PASSWORD;
    private static String url = "tcp://localhost:61616";

    public static void main(String[] args) throws Exception {
        // ConnectionFactory :连接工厂,JMS 用它创建连接
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                user, password, url);
        // Connection :JMS 客户端到JMS Provider 的连接
        Connection connection = connectionFactory.createConnection();
        // Connection 启动
        connection.start();
        System.out.println("Connection is start...");
        // Session: 一个发送或接收消息的线程
        Session session = connection.createSession(Boolean.TRUE,
                Session.AUTO_ACKNOWLEDGE);
        // Topicr :消息的目的地;消息发送给谁.
        Topic destination = session.createTopic("example.A");
        // MessageProducer:消息发送者
        MessageProducer producer = session.createProducer(destination);
        // 设置不持久化,此处学习,实际根据项目决定
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        // 构造消息,此处写死,项目就是参数,或者方法获取
        sendMessage(session, producer);
        session.commit();

        connection.close();
        System.out.println("send text ok.");
    }

    public static void sendMessage(Session session, MessageProducer producer)
            throws Exception {
        for (int i = 1; i <= 100; i++) {// 有限制,达到1000就不行
            TextMessage message = session.createTextMessage("ActiveMq 发送的消息"
                    + i);
            // 发送消息到目的地方
            System.out.println("发送消息:" + "ActiveMq 发送的消息" + i);
            producer.send(message);
        }
    }

}

订阅者

import javax.jms.Connection;  
import javax.jms.ConnectionFactory;  
import javax.jms.JMSException;  
import javax.jms.Message;  
import javax.jms.MessageConsumer;  
import javax.jms.MessageListener;  
import javax.jms.Session;  
import javax.jms.TextMessage;  
import javax.jms.Topic;  

import org.apache.activemq.ActiveMQConnection;  
import org.apache.activemq.ActiveMQConnectionFactory;  

public class TopicSubscriber {

    private static String user = ActiveMQConnection.DEFAULT_USER;  
    private static String password =ActiveMQConnection.DEFAULT_PASSWORD;  
    private static String url = "tcp://localhost:61616";  
    public static void main(String[] args) throws Exception{  
        // ConnectionFactory :连接工厂,JMS 用它创建连接  
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user,password,url);  
        // Connection :JMS 客户端到JMS Provider 的连接  
        Connection connection = connectionFactory.createConnection();  
        connection.start();  
        // Session: 一个发送或接收消息的线程  
        final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);  
        // Destination :消息的目的地;消息发送给谁.  
        Topic destination=session.createTopic("example.A");  
        // 消费者,消息接收者  
        MessageConsumer consumer = session.createConsumer(destination);  
        consumer.setMessageListener(new MessageListener(){//有事务限制  
            @Override  
            public void onMessage(Message message) {  
                try {  
                    TextMessage textMessage=(TextMessage)message;  
                    System.out.println(textMessage.getText());  
                } catch (JMSException e1) {  
                    e1.printStackTrace();  
                }  
                try {  
                    session.commit();  
                } catch (JMSException e) {  
                    e.printStackTrace();  
                }  
            }  
        });  

/*  另外一种接受方式 
 *    while (true) { 
              //设置接收者接收消息的时间,为了便于测试,这里谁定为100s 
              TextMessage message = (TextMessage) consumer.receive(100000); 
              if (null != message) { 
                  System.out.println("收到消息" + message.getText()); 
              } else { 
                  break; 
              } 
          }*/  
    }  

}

Queue方式和Topic方式区别
在Queue(点对点)方式中先运行生产者,再运行消费者,消费者还能接受到消息;
而Topic(发布/订阅)方式就不同了,先运行发布者,再运行订阅者,订阅者收到的消息可能没有或者是不完全的。

你可能感兴趣的:(消息队列)