ActiveMQ 队列模式(一)

消息队列学习,首先下载 http://activemq.apache.org/activemq-5150-release.html
下载完成后 解压文件 本文采用的windows 启动目录apache-activemq-5.12.0\bin\win32
里面有启动版 和 安装服务版 启动成功后

wrapper  | --> Wrapper Started as Console
wrapper  | Launching a JVM...
jvm 1    | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
jvm 1    |   Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
jvm 1    |
jvm 1    | Java Runtime: Oracle Corporation 1.7.0_80 C:\Program Files\Java\jre7
jvm 1    |   Heap sizes: current=15872k  free=12418k  max=1013632k
jvm 1    |     JVM args: -Dactivemq.home=../.. -Dactivemq.base=../.. -Djavax.net
.ssl.keyStorePassword=password -Djavax.net.ssl.trustStorePassword=password -Djav
ax.net.ssl.keyStore=../../conf/broker.ks -Djavax.net.ssl.trustStore=../../conf/b
roker.ts -Dcom.sun.management.jmxremote -Dorg.apache.activemq.UseDedicatedTaskRu
nner=true -Djava.util.logging.config.file=logging.properties -Dactivemq.conf=../
../conf -Dactivemq.data=../../data -Djava.security.auth.login.config=../../conf/
login.config -Xmx1024m -Djava.library.path=../../bin/win32 -Dwrapper.key=XOdiDWx
m0c5XBWJr -Dwrapper.port=32000 -Dwrapper.jvm.port.min=31000 -Dwrapper.jvm.port.m
ax=31999 -Dwrapper.pid=2132 -Dwrapper.version=3.2.3 -Dwrapper.native_library=wra
pper -Dwrapper.cpu.timeout=10 -Dwrapper.jvmid=1
jvm 1    | Extensions classpath:
jvm 1    |   [..\..\lib,..\..\lib\camel,..\..\lib\optional,..\..\lib\web,..\..\l
ib\extra]
jvm 1    | ACTIVEMQ_HOME: ..\..
jvm 1    | ACTIVEMQ_BASE: ..\..
jvm 1    | ACTIVEMQ_CONF: ..\..\conf
jvm 1    | ACTIVEMQ_DATA: ..\..\data
jvm 1    | Loading message broker from: xbean:activemq.xml
jvm 1    |  INFO | Refreshing org.apache.activemq.xbean.XBeanBrokerFactory$1@14e
e9dd: startup date [Sun Sep 17 10:06:12 CST 2017]; root of context hierarchy
jvm 1    |  INFO | PListStore:[D:\apache-activemq-5.12.0\bin\win32\..\..\data\lo
calhost\tmp_storage] started
jvm 1    |  INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[D:\apache
-activemq-5.12.0\bin\win32\..\..\data\kahadb]
jvm 1    |  INFO | Apache ActiveMQ 5.12.0 (localhost, ID:julong-841deb9b-2051-15
05613974902-0:1) is starting
jvm 1    |  INFO | Listening for connections at: tcp://julong-841deb9b:61616?max
imumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector openwire started
jvm 1    |  INFO | Listening for connections at: amqp://julong-841deb9b:5672?max
imumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector amqp started
jvm 1    |  INFO | Listening for connections at: stomp://julong-841deb9b:61613?m
aximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector stomp started
jvm 1    |  INFO | Listening for connections at: mqtt://julong-841deb9b:1883?max
imumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector mqtt started
jvm 1    | {}
jvm 1    |  INFO | Listening for connections at ws://julong-841deb9b:61614?maxim
umConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector ws started
jvm 1    |  INFO | Apache ActiveMQ 5.12.0 (localhost, ID:julong-841deb9b-2051-15
05613974902-0:1) started
jvm 1    |  INFO | For help or more information please see: http://activemq.apac
he.org
jvm 1    |  WARN | Store limit is 102400 mb (current store usage is 0 mb). The d
ata directory: D:\apache-activemq-5.12.0\bin\win32\..\..\data\kahadb only has 66
84 mb of usable space - resetting to maximum available disk space: 6684 mb
jvm 1    |  WARN | Temporary Store limit is 51200 mb, whilst the temporary data
directory: D:\apache-activemq-5.12.0\bin\win32\..\..\data\localhost\tmp_storage
only has 6684 mb of usable space - resetting to maximum available 6684 mb.
jvm 1    |  INFO | ActiveMQ WebConsole available at http://0.0.0.0:8161/
jvm 1    |  INFO | ActiveMQ Jolokia REST API available at http://0.0.0.0:8161/ap
i/jolokia/
jvm 1    |  INFO | Initializing Spring FrameworkServlet 'dispatcher'
jvm 1    |  INFO | jolokia-agent: No access restrictor found at classpath:/jolok
ia-access.xml, access to all MBeans is allowed
jvm 1    |  INFO | Connector vm://localhost started

在浏览器输入 http://127.0.0.1:8161/
点击 Manage ActiveMQ broker 默认登录名和密码为 admin:admin

不说了实践下就明白了

	<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>5.12.0</version>
</dependency>

在maven中加入此配置

消息创建者

package com.julongtech.queue;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;


/**
 * 消息创建者
 *
 */
public class App 
{

	/**
	 * 创建地址
	 * @author julong
	 * @date 2017-9-17 上午10:55:56
	 */
	private static final String url = "tcp://127.0.0.1:61616";
	/**
	 * 创建队列名称
	 * @author julong
	 * @date 2017-9-17 上午10:56:56
	 */
	private static final String queueName = "queue-test";

	/**
	 * @param args
	 * @author julong
	 * @throws JMSException 
	 * @date 2017-9-17 上午10:59:43
	 */
	public static void main( String[] args ) throws JMSException{
		//创建连接工厂
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
		//创建连接对象
	    Connection connection = connectionFactory.createConnection();
	    //启动连接
	    connection.start();
	   //创建回话对象
	    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	    
	    //创建一个目标地址                               创建消息队列
	    Destination destination = session.createQueue(queueName);
	    
	    //创建一个消息生产者
	    MessageProducer messageProducer = session.createProducer(destination);
	    
	    //消息发送
	    for (int i = 0; i < 50; i++) {
			//创建消息
	    	TextMessage textMessage = session.createTextMessage("第"+i+"次消息!");
	    	//发送消息
	    	messageProducer.send(textMessage);
	    	
	    	System.out.println("发送消息:"+textMessage);
		}
	    //关闭连接
	    connection.close();
		
	}
}
 com.julongtech.queue;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;


/**
 * 消息创建者
 *
 */
public class App 
{

	/**
	 * 创建地址
	 * @author julong
	 * @date 2017-9-17 上午10:55:56
	 */
	private static final String url = "tcp://127.0.0.1:61616";
	/**
	 * 创建队列名称
	 * @author julong
	 * @date 2017-9-17 上午10:56:56
	 */
	private static final String queueName = "queue-test";

	/**
	 * @param args
	 * @author julong
	 * @throws JMSException 
	 * @date 2017-9-17 上午10:59:43
	 */
	public static void main( String[] args ) throws JMSException{
		//创建连接工厂
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
		//创建连接对象
	    Connection connection = connectionFactory.createConnection();
	    //启动连接
	    connection.start();
	   //创建回话对象
	    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	    
	    //创建一个目标地址                               创建消息队列
	    Destination destination = session.createQueue(queueName);
	    
	    //创建一个消息生产者
	    MessageProducer messageProducer = session.createProducer(destination);
	    
	    //消息发送
	    for (int i = 0; i < 50; i++) {
			//创建消息
	    	TextMessage textMessage = session.createTextMessage("第"+i+"次消息!");
	    	//发送消息
	    	messageProducer.send(textMessage);
	    	
	    	System.out.println("发送消息:"+textMessage);
		}
	    //关闭连接
	    connection.close();
		
	}
}

消息消费者


package com.julongtech.queue;

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.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 消息消费者
 * @author julong
 * @date 2017-9-17 上午11:38:08
 */
public class Consumer {

	/**
	 * 创建地址
	 * @author julong
	 * @date 2017-9-17 上午10:55:56
	 */
	private static final String url = "tcp://127.0.0.1:61616";
	/**
	 * 创建队列名称
	 * @author julong
	 * @date 2017-9-17 上午10:56:56
	 */
	private static final String queueName = "queue-test";
	/**
	 * @param args
	 * @author julong
	 * @throws JMSException 
	 * @date 2017-9-17 上午11:23:38
	 */
	public static void main(String[] args) throws JMSException {
		// TODO Auto-generated method stub
		//创建连接工厂
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
		//创建连接对象
	    Connection connection = connectionFactory.createConnection();
	    //启动连接
	    connection.start();
	   //创建回话对象
	    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	    //创建一个目标地址                               创建消息队列
	    Destination destination = session.createQueue(queueName);
	    //创建消息消费者
	    MessageConsumer messageConsumer = session.createConsumer(destination);
	    
	    //创建消息消费监听
	    messageConsumer.setMessageListener(new MessageListener() {
			public void onMessage(Message message) {
				// TODO Auto-generated method stub
				TextMessage textMessage = (TextMessage) message;
				try {
					System.out.println("接收消息"+textMessage.getText());
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
//		connection.close();
	}
} com.julongtech.queue;

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.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * 消息消费者
 * @author julong
 * @date 2017-9-17 上午11:38:08
 */
public class Consumer {

	/**
	 * 创建地址
	 * @author julong
	 * @date 2017-9-17 上午10:55:56
	 */
	private static final String url = "tcp://127.0.0.1:61616";
	/**
	 * 创建队列名称
	 * @author julong
	 * @date 2017-9-17 上午10:56:56
	 */
	private static final String queueName = "queue-test";
	/**
	 * @param args
	 * @author julong
	 * @throws JMSException 
	 * @date 2017-9-17 上午11:23:38
	 */
	public static void main(String[] args) throws JMSException {
		// TODO Auto-generated method stub
		//创建连接工厂
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
		//创建连接对象
	    Connection connection = connectionFactory.createConnection();
	    //启动连接
	    connection.start();
	   //创建回话对象
	    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	    //创建一个目标地址                               创建消息队列
	    Destination destination = session.createQueue(queueName);
	    //创建消息消费者
	    MessageConsumer messageConsumer = session.createConsumer(destination);
	    
	    //创建消息消费监听
	    messageConsumer.setMessageListener(new MessageListener() {
			public void onMessage(Message message) {
				// TODO Auto-generated method stub
				TextMessage textMessage = (TextMessage) message;
				try {
					System.out.println("接收消息"+textMessage.getText());
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
//		connection.close();
	}
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值