创建连接工厂对象
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(tcp://localhost:61616);
从工厂获取连接
Connection connection = connectionFactory.createConnection()
连接MQ服务
connection.start();
获取session连接对象
//是否开启事务,签收模式,这里是不开启事务和自动应答
Session session = connection.createSession(Boolean.FALSE, Session.CLIENT_ACKNOWLEDGE);
通过Session获取Topic
Topic topic = session.createTopic("zzfTopic")
通过session创建消息的发送者
MessageProducer messageProducer = session.createProducer(topic );
通过session创建消息对象
TextMessage textMessage = session.createTextMessage("ping")
发送消息
messageProducer .send(textMessage );
关闭资源
messageProducer .close();
session.close();
commection.close();
创建连接工厂对象
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(tcp://localhost:61616);
从工厂获取连接
Connection connection = connectionFactory.createConnection()
连接MQ服务
connection.start();
获取session连接对象
//是否开启事务,签收模式,这里是不开启事务和自动应答
Session session = connection.createSession(Boolean.FALSE, Session.CLIENT_ACKNOWLEDGE);
通过Session获取Topic
Topic topic = session.createTopic("zzfTopic")
通过session创建消息的发送者
MessageConsumer consumer = session.createConsumer(topic );
指定消息监听器
consumer.setMessageListener(new MessageListener){
public void onMessage(Message message){
TextMessage textMessage = (TextMessage) message;
System.out.println(textMessage.getText)
//手工去签收消息,另起一个线程(TCP)去通知我们的MQ服务,确认签收。
textMessage.acknowledge();
}
连接资源不用关闭,接受者需要持续运行。