1.从官网下载activeMQ,网址http://activemq.apache.org/activemq-5141-release.html,随便找一个版本下载都行,windows点击一下连接下载。
2.启动activeMQ,进入到下载的目录,点击activemq.bat,进行启动,
3.如果出现启动失败,一般是因为端口被占用的原因,win10端口被占用的解决方法如下:https://jingyan.baidu.com/article/3c48dd34491d47e10be358b8.html,我一开始是端口被占用启动失败的,然后一切问题解决。
4.在浏览器输入http://localhost:8161/进入后台管理页面,输入账号密码admin admin (默认)
5.开始撸代码,provider和consumer的代码如下:
public class DNProviderImpl implements DNProvider{
private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;
ConnectionFactory connectionFactory;
Connection connection;
Session session;
public void init() {
try {
connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKERURL);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(true, Session.SESSION_TRANSACTED);
} catch (JMSException e) {
e.printStackTrace();
}
}
Connection connection;
Session session;
public void init() {
try {
connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKERURL);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(true, Session.SESSION_TRANSACTED);
} catch (JMSException e) {
e.printStackTrace();
}
}
public void sendMessage(String disName) {
//创建队列
try {
Queue queue = session.createQueue(disName);
MessageProducer messageProducer = session.createProducer(queue);
for(int i=0;i<10;i++){
TextMessage tm = session.createTextMessage("provider activeMq 发送短信,内容是content");
System.out.println("provider activeMq 发送短信,内容是content");
//发送消息
messageProducer.send(tm);
//提交
session.commit();
}
} catch (JMSException e) {
e.printStackTrace();
}
}
//创建队列
try {
Queue queue = session.createQueue(disName);
MessageProducer messageProducer = session.createProducer(queue);
for(int i=0;i<10;i++){
TextMessage tm = session.createTextMessage("provider activeMq 发送短信,内容是content");
System.out.println("provider activeMq 发送短信,内容是content");
//发送消息
messageProducer.send(tm);
//提交
session.commit();
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.dongnao.jack.queue.consumer;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* @ClassName: DNConsumerImpl
* @Description: TODO
* @author: Taurus
* @date: 2018年1月4日 下午5:29:08
*/
public class DNConsumerImpl implements DNConsumer {
private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;
* @ClassName: DNConsumerImpl
* @Description: TODO
* @author: Taurus
* @date: 2018年1月4日 下午5:29:08
*/
public class DNConsumerImpl implements DNConsumer {
private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;
ConnectionFactory connectionFactory;
Connection connection;
Session session;
public void init() {
try {
connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKERURL);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e) {
e.printStackTrace();
}
}
Connection connection;
Session session;
public void init() {
try {
connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKERURL);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e) {
e.printStackTrace();
}
}
public void getMessage(String disName) {
try {
Queue queue = session.createQueue(disName);
MessageConsumer messageConsumer = session.createConsumer(queue);
while(true){
//接收消息
TextMessage message = (TextMessage) messageConsumer.receive();
Thread.sleep(1000);
if(message!=null){
System.out.println("consumer :我是消费者,我接收的消息内容是:"+message.getText());
}else{
break;
}
}
} catch (JMSException | InterruptedException e) {
e.printStackTrace();
}
}
try {
Queue queue = session.createQueue(disName);
MessageConsumer messageConsumer = session.createConsumer(queue);
while(true){
//接收消息
TextMessage message = (TextMessage) messageConsumer.receive();
Thread.sleep(1000);
if(message!=null){
System.out.println("consumer :我是消费者,我接收的消息内容是:"+message.getText());
}else{
break;
}
}
} catch (JMSException | InterruptedException e) {
e.printStackTrace();
}
}
}
6.编写测试类
public class ProviderTest {
public static void main(String[] args) {
DNProvider provider = new DNProviderImpl();
provider.init();
provider.sendMessage("DN_JACK_20");
}
}
DNProvider provider = new DNProviderImpl();
provider.init();
provider.sendMessage("DN_JACK_20");
}
}
public class ConsumerTest {
public static void main(String[] args) {
DNConsumer consumer = new DNConsumerImpl();
consumer.init();
consumer.getMessage("DN_JACK_20");
}
}
DNConsumer consumer = new DNConsumerImpl();
consumer.init();
consumer.getMessage("DN_JACK_20");
}
}
7.实验结果
8.activeMQ后台查看,消息名称,消费者个数都一 一显示了