如何使用java程序操作ActiveMQ

第一步:开发activeMQ要先导入activemq-all-5.14.0.jar包,因为我创建的是maven 项目,因此需要使用maven坐标导入。如果创建的是普通java项目,则应该在lib目录下导入jar包。

<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.niwotaxuexiba.maven</groupId>
  <artifactId>activeMQ_helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>activeMQ_helloworld</name>
  <dependencies>
  	<dependency>
  		<groupId>org.apache.activemq</groupId>
  		<artifactId>activemq-all</artifactId>
  		<version>5.14.0</version>
  	</dependency>
  </dependencies>
</project>

第二步:因为要使用JUnit测试,所以也需要将JUnit的jar包也导入pom文件。

<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<version>4.12</version>
</dependency>

第三步:编写MQ消息生产者。分为几个小的步骤:

①连接工厂。

②选择使用默认的用户名/密码/路径。

③new一个ActiveMQConnectionFactory对象,也就是实例化一个工厂,这是使用ActiveMQ的入口。

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();

④获取一个连接

Connection connection = connectionFactory.createConnection();

⑤建立会话

Session session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);

⑥创建队列或者话题对象

Queue queue = session.createQueue("HelloWorld");

⑦创建生产者或者消费者

MessageProducer producer = session.createProducer(queue);

⑧发送消息

for(int i = 0; i < 10; i++){

producer.send(session.createTextMessage("你好,activeMQ:"+i));

}

⑨提交操作

session.commit();

public class ActiveMQProducer {
	@Test
	public void testProduceMQ() throws Exception {
		// 连接工厂
		// 使用默认用户名、密码、路径
		// 路径 tcp://host:61616
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
		// 获取一个连接
		Connection connection = connectionFactory.createConnection();
		// 建立会话
		Session session = connection.createSession(true,
				Session.AUTO_ACKNOWLEDGE);
		// 创建队列或者话题对象
		Queue queue = session.createQueue("HelloWorld");
		// 创建生产者 或者 消费者
		MessageProducer producer = session.createProducer(queue);

		// 发送消息
		for (int i = 0; i < 10; i++) {
			producer.send(session.createTextMessage("你好,activeMQ:" + i));
		}
		// 提交操作
		session.commit();

	}
}

查看控制台,消息已经被生产


第四步:编写MQ消费者代码。

①使用MessageConsumer完成消费:

public class ActiveMQConsumer {

	@Test
	// 直接消费
	public void testCosumeMQ() throws Exception {
		// 连接工厂
		// 使用默认用户名、密码、路径
		// 路径 tcp://host:61616
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
		// 获取一个连接
		Connection connection = connectionFactory.createConnection();
		// 开启连接
		connection.start();
		// 建立会话
		// 第一个参数,是否使用事务,如果设置true,操作消息队列后,必须使用 session.commit();
		Session session = connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		// 创建队列或者话题对象
		Queue queue = session.createQueue("HelloWorld");
		// 创建消费者
		MessageConsumer messageConsumer = session.createConsumer(queue);

		while (true) {
			TextMessage message = (TextMessage) messageConsumer.receive(10000);
			if (message != null) {
				System.out.println(message.getText());
			} else {
				break;
			}
		}
	}
}

查看控制台,发现信息已经被消费

②使用监听器,监听消息的内容,进行消费。

@Test
	// 使用监听器消费
	public void testCosumeMQ2() throws Exception {
		// 连接工厂
		// 使用默认用户名、密码、路径
		// 路径 tcp://host:61616
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
		// 获取一个连接
		Connection connection = connectionFactory.createConnection();
		// 开启连接
		connection.start();
		// 建立会话
		// 第一个参数,是否使用事务,如果设置true,操作消息队列后,必须使用 session.commit();
		Session session = connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		// 创建队列或者话题对象
		Queue queue = session.createQueue("HelloWorld");
		// 创建消费者
		MessageConsumer messageConsumer = session.createConsumer(queue);

		messageConsumer.setMessageListener(new MessageListener() {
			// 每次接收消息,自动调用 onMessage
			public void onMessage(Message message) {
				TextMessage textMessage = (TextMessage) message;
				try {
					System.out.println(textMessage.getText());
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		});

		while (true) {
			// 不能让junit线程死掉
		}
	}


以上就是activeMQ消息的生产及效果的代码编写。


  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学亮编程手记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值