ActiveMQ入门笔记(1)

1,ActiveMQ下载

2,开始ActiveMQ

(1) 在控制台cd [activimq_install_dir]/bin/activemq start

(2) 在浏览器进入http://localhost:8161/admin到ActiveMQ的管理页面 账号:admin 密码:admin
(3) 切换到Queues导航,创建一个Queue,输入一个name点击create
(4)点击send to输入MessageBody内容点击send可以在

[activimq_install_dir]/data/activemq.log中出现

Apache ActiveMQ 5.14.0 (localhost, ID:DESKTOP-FBFV2OK-57402-1472799279718-0:1) is starting | org.apache.activemq.broker.BrokerService | main

3,创建java项目

package com.lql.activimqtest;

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

import org.apache.activemq.ActiveMQConnectionFactory;

public class Producer {
	public static void main(String[] args) throws JMSException {
		ConnectionFactory connectionFactory;
		Connection connection = null;
		Session session = null;
		MessageConsumer consumer = null;
		try {
            // Create a ConnectionFactory
            connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");

            // Create a Connection
            connection = connectionFactory.createConnection();
            connection.start();

            // Create a Session
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create the destination (Topic or Queue)
            Destination destination = session.createQueue("MyQueue");

            // Create a MessageProducer from the Session to the Topic or Queue
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            
            sendMessage(session, producer);
        }
        catch (Exception e) {
            System.out.println("Caught: " + e);
            e.printStackTrace();
        } finally {
        	session.close();
            connection.close();
        }
	}

	private static void sendMessage(Session session, MessageProducer producer) throws  Exception {
		// TODO Auto-generated method stub
		for(int i = 0; i < 10; i++) {
			// 这里将线程停顿1s,为了使结果更明显
			Thread.sleep(1000);
			String text = "Hello world! From: Producer " + i;
			TextMessage message = session.createTextMessage(text);
			// Tell the producer to send the message
            System.out.println("Sent message: "+ i);
            producer.send(message);
		}
	}
}


package com.lql.activimqtest;

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

import org.apache.activemq.ActiveMQConnectionFactory;

public class Consumer {
	public static void main(String[] args) throws JMSException {
		ConnectionFactory connectionFactory;
		Connection connection = null;
		Session session = null;
		MessageConsumer consumer = null;
		
		// Create a ConnectionFactory
		connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
		try {

            // Create a Connection
            connection = connectionFactory.createConnection();
            connection.start();

            // Create a Session
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create the destination (Topic or Queue)
            Destination destination = session.createQueue("MyQueue");

            // Create a MessageConsumer from the Session to the Topic or Queue
            consumer = session.createConsumer(destination);

            while(true) {
	            // Wait for a message
	            Message message = consumer.receive(100);
	
	            if (message instanceof TextMessage) {
	                TextMessage textMessage = (TextMessage) message;
	                String text = textMessage.getText();
	                System.out.println("Received: " + text);
	            }
			}
            
        } catch (Exception e) {
            System.out.println("Caught: " + e);
            e.printStackTrace();
        } finally {
        	consumer.close();
            session.close();
            connection.close();
        }
	}
}

4,运行项目

首先运行Consumer,控制台会处于等待状态
然后运行Producer,控制台每秒输出一次,同时Consumer的控制台也会出现打印输出
运行结果如图
Producer:
Consumer
在http://localhost:8161/admin/queues.jsp中可以看到
一共发送10条消息,接收10条消息
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值