ActiveMQ-toptic主题模式

1.消费者

package com.touch.theme;
import cn.hutool.core.util.RandomUtil;
import com.touch.utils.ActiveMQUtil;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.util.Date;
/**
 * 消费者
 */
public class TestConsumer {
	//服务地址,端口默认61616
	private static final String url = "tcp://127.0.0.1:61616";
	//这次消费的消息名称
	private static final String topicName = "queue_style";
	//消费者有可能是多个,为了区分不同的消费者,为其创建随机名称
	private static final String consumerName = "consumer-" + RandomUtil.randomString(5);

	public static void main(String[] args) throws JMSException {
		//0. 先判断端口是否启动了 Active MQ 服务器
		ActiveMQUtil.checkServer();
		System.out.printf("%s 消费者启动了。 %n", consumerName);
		//1.创建ConnectiongFactory,绑定地址
		ConnectionFactory factory = new ActiveMQConnectionFactory(url);
		//2.创建Connection
		Connection connection = factory.createConnection();
		//3.启动连接
		connection.start();
		//4.创建会话
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//5.创建一个目标 (主题类型) 这里是 createTopic 而 队列模式是 createQueue,只有这一个地方不同;
		Destination destination = session.createTopic(topicName);
		//6.创建一个消费者
		MessageConsumer consumer = session.createConsumer(destination);
		//7.创建一个监听器
		consumer.setMessageListener(new MessageListener() {
			public void onMessage(Message arg0) {
				TextMessage textMessage = (TextMessage) arg0;
				try {
					System.out.println(new Date()+":"+consumerName + " 主题类型接收消息:" + textMessage.getText());
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		//8. 因为不知道什么时候有,所以没法主动关闭,就不关闭了,一直处于监听状态
		//connection.close();

	}
}

2.生产者

package com.touch.theme;
import com.touch.utils.ActiveMQUtil;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.util.Date;
/**
 * 生产者
 */
public class TestProducer {
	//服务地址,端口默认61616
	private static final  String url="tcp://127.0.0.1:61616";
	private static final  String topicName="queue_style";
	public static void main(String[] args) throws JMSException {
		ActiveMQUtil.checkServer();
		//1.创建ConnectionFactory,绑定地址
		ConnectionFactory factory = new ActiveMQConnectionFactory(url);
		//2.创建Connection
		Connection connection = factory.createConnection();
		//3.启动连接
		connection.start();
		//4.创建会话
		Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
		/*5.创建一个目标 (主题类型) 和队列模式的
		  TestProducer几乎一模一样,只有一个地方有区别:
		  Destination destination=session.createTopic(topicName);
		  这里是 createTopic 而 队列模式是 createQueue*/
		Destination destination=session.createTopic(topicName);
		//6.创建一个生产者
		MessageProducer producer=session.createProducer(destination);
		for (int i = 0; i <100 ; i++) {
			//7.创建消息
			TextMessage textMessage = session.createTextMessage("主题消息-"+i);
			//8.发送消息
			producer.send(textMessage);
			System.out.println(new Date()+":"+ "发送 = " +textMessage.getText());
		}
		//9.关闭连接
		connection.close();
	}
}

[源代码地址:https://github.com/hyghub/javanotes]

你可能感兴趣的:(消息中间件)