ActionMQ 小试牛刀

一:先预身一下

1:ActiveMQ的安装

ActionMQ的安装三步曲
1:进入http://activemq.apache.org/下载,把ActiveMQ的压缩包放到D://中
2:解压
3:启动 D:\apache-activemq-5.6.0\bin\win64\activemq



打开浏览器输入:http://localhost:8161/admin/


能访问到这个页面说明 ActiveMQ 安装完成病正常运行了 下面我们来测试一下:

第一步:建立一个maven工程;



第二步:引入相关jar包

    <dependency>
	  <groupId>org.apache.activemq</groupId>
	  <artifactId>activemq-all</artifactId>
	  <version>5.9.0</version>
    </dependency>
	
    <dependency>
	  <groupId>org.apache.xbean</groupId>
	  <artifactId>xbean-spring</artifactId>
	  <version>3.16</version>
    </dependency>

第三步:创建 SentMessage 发送方(生产者类 )和 ReceiveMessage 接收方(消费者)

public class SentMessage {

	public static void main(String[] args) throws JMSException {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.21.77:61616");
		Connection connection;
		connection = connectionFactory.createConnection();
		connection.start();
		
		Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
		Destination destination = session.createQueue("my-queue");
		try {
		    MessageProducer producer = session.createProducer(destination);
		    for (int i = 0; i < 3; i++) {
		        TextMessage message = session.createTextMessage("message--" + i);
	       
		        // 通过
		        producer.send(message);
		    }
		} catch (JMSException e) {
			e.printStackTrace();
		}finally{
			session.commit();
			session.close();
			connection.close();
			
		}
	
	}

第四步:运行 SentMessage 发送方(生产者类 )




可以看到 我发送了3条数据 MessagesDequeued 是我消息接受的数目 这里我并没有运行 接受方 所以数目为0


第五步:运行 ReceiveMessage 接收方(消费者) 



可以看到在控制台中 打印了 接收 发送方发送的3条数据,我们再来看看刚才的MessagesDequeued 的条数有没有变化


哇 太牛 13 了 哈哈 小牛初始成功。。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值