ActiveMQ消息队列

ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。支持很多跨语言的客户端和协议,例如 Java, C, C++, C#, Ruby, Perl, Python, PHP。支持的协议有OpenWire ,Stomp,AMQP,MQTT。

Unix安装启动

选择要下载的ActiveMQ 版本,名称类似于 activemq-x.x.x-tar.gz,然后启动

tar zxvf activemq-x.x.x.tar.gz

cd [activemq-install_dir]/bin

chmod 755 activemq

cd [activemq_install_dir]

bin\activemq start


监控ActiveMQ运行状态的地址

http://localhost:8161/admin


停止ActiveMQ

cd [activemq_install_dir]/bin

./activemq stop


JAVA客户端代码

发送消息到ActiveMQ队列的代码:

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
 
import org.apache.activemq.ActiveMQConnectionFactory;
 
public final class ActiveMQSample {
 
  public static void main(final String[] args) throws Exception {
    final ConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://192.168.10.201:61616");
 
    final Connection conn = connFactory.createConnection();
 
    final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
 
    final Destination dest = sess.createQueue("SampleQueue");
 
    final MessageProducer prod = sess.createProducer(dest);
 
    final Message msg = sess.createTextMessage("Simples Assim");
 
    prod.send(msg);
 
    conn.close();
  }
 
}

发送消息到 ActiveMQ主题的代码:

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
 
import org.apache.activemq.ActiveMQConnectionFactory;
 
public final class ActiveMQSample {
 
  public static void main(final String[] args) throws Exception {
    final ConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://192.168.10.201:61616");
 
    final Connection conn = connFactory.createConnection();
 
    final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
 
    final Destination dest = sess.createTopic("SampleTopic");
 
    final MessageProducer prod = sess.createProducer(dest);
 
    final Message msg = sess.createTextMessage("Simples Assim");
 
    prod.send(msg);
 
    conn.close();
  }
 
}


ActiveMQ队列接收消息的代码:

package com.zhuyun.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQConsumer {
	public static void main(String[] args) throws Exception{

		final ConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://192.168.10.201:61616");
		final Connection conn = connFactory.createConnection();
		conn.start();
		final Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
		
		final Destination dest = session.createQueue("SampleQueue");

		
		final MessageConsumer consumer = session.createConsumer(dest);
		Message message = consumer.receive();
		conn.close();
		
	}
}

ActiveMQ主题接收消息的代码:

package com.zhuyun.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQConsumer {
	public static void main(String[] args) throws Exception{

		final ConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://192.168.10.201:61616");
		final Connection conn = connFactory.createConnection();
		conn.start();
		final Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
		
		final Destination dest = session.createTopic("SampleTopic");
		
		final MessageConsumer consumer = session.createConsumer(dest);
		Message message = consumer.receive();
		conn.close();
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值