activemq的搭建、启动,简单demo

时间:2023-03-09 19:46:29
activemq的搭建、启动,简单demo

一、搭建activeMQ

  在官网下载window版本,直接解压就可以。

二、启动

  在解压完的目录/bin/win64,双击击activemq.bat,运行完之后打开浏览器,输入http://127.0.0.1:8161/admin/,出现以下页面说明成功。

  activemq的搭建、启动,简单demo

三、简单DEMO(使用点对点的模式演示的)

  本DEMO是在maven环境下操作的,没有maven的请先安装maven

  1:在pom.xml添加activeMQ的jar,在dependencies标签中添加如下,因为本人的MQ版本是5.8,所以mvn的MQ也是5.8

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

  2:创建生产者

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class ActiveMQProducer { /*设置默认的用户名*/
private static final String USERNAME =
ActiveMQConnection.DEFAULT_USER;
/*设置默认的密码*/
private static final String PASSWORD =
ActiveMQConnection.DEFAULT_PASSWORD;
/*设置默认的连接地址*/
private static final String BROKEURL =
ActiveMQConnection.DEFAULT_BROKER_URL; public static void main(String[] args) {
ConnectionFactory connectionFactory;
Connection connection = null;
try {
/*创建连接工厂*/
connectionFactory =
new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURL);
/*创建连接,并且启动*/
connection = connectionFactory.createConnection();
connection.start();
/*创建一个回话*/
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
/*消息的目的地*/
Destination destination = session.createQueue("hello-world");
/*消息的生产者*/
MessageProducer messageProducer = session.createProducer(destination);
/*发送消息*/
for (int i = 1; i < 4; i++) {
String msg = "发送第"+i+"条消息";
System.out.println(msg);
TextMessage textMessage = session.createTextMessage(msg);
messageProducer.send(textMessage);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace(); }
}
}
}
}

  3:创建消费者

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class ActiveMQConsumer { /*设置默认的用户名*/
private static final String USERNAME =
ActiveMQConnection.DEFAULT_USER;
/*设置默认的密码*/
private static final String PASSWORD =
ActiveMQConnection.DEFAULT_PASSWORD;
/*设置默认的连接地址*/
private static final String BROKEURL =
ActiveMQConnection.DEFAULT_BROKER_URL; public static void main(String[] args) {
ConnectionFactory connectionFactory;
Connection connection = null;
try {
/*创建连接工厂*/
connectionFactory =
new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURL);
/*创建连接,并且启动*/
connection = connectionFactory.createConnection();
connection.start();
/*创建一个回话*/
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
/*消息的目的地*/
Destination destination = session.createQueue("hello-world");
/*消息的消费者*/
MessageConsumer messageConsumer = session.createConsumer(destination);
/*获取消息*/
Message message;
while ((message = messageConsumer.receive())!=null){
System.out.println(((TextMessage)message).getText());
};
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace(); }
}
}
} }  

  直接运行main方法,先启动消费或生产者都可以。

注:

 区别:

  queue:是点对点的模式,一个生产者对应一个消费者,消息不会被重复的消费

  topic:主题或者发布订阅模式,一个生产者对应多个消费者,消息会被重复的消费。

 代码:生产者和消费者都需要修改

  Destination destination = session.createQueue("hello-world");//点对点模式

  Destination destination = session.createTopic("hello-world");//主题模式