标签:连接 ati you get str static 消息 create rod
特点:
+ 一个消费生产者必须有一个消息消费者。一对一的关系
+ 一个消息发送到queue中,如果mqserver重启,消息不会丢失(当然也可以设置为丢失。缺省是不会丢失的)
特点:
+ 一个生产者生产的消息可以同时被多个消息消费者消费。一对多。
+ 一个消费者可以消费来自不同生产者的消息。
package com.hs.gz.hellodemo.mq.demo;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQMessage;
/**
* 生产者
* @author hasee
*
*/
public class Producer {
private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
private static final String BROKEURL = "tcp://139.199.158.112:61616";
private static final int SENDNUM = 3;
public static void main(String[] args) throws Exception {
//工厂
ConnectionFactory factory;
//连接
Connection connection;
//会话
Session session;
//目的地
Destination destination;
//消费者
MessageProducer producer;
//指定用户名,密码和url来创建连接工厂
factory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEURL);
//从连接工厂中获取丽连接
connection = factory.createConnection();
//从连接中创建session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
/* 创建一个名为HelloWorld消息队列*/
destination = session.createQueue("HelloWorld");
/*往队列里面注册生产者*/
producer = session.createProducer(destination);
for (int i = 0; i < SENDNUM; i++) {
String msg = "发送消息"+i+" "+System.currentTimeMillis();
TextMessage textMessage = session.createTextMessage(msg);
producer.send(textMessage);
}
System.out.println("生成者生产ok.....");
producer.close();
session.close();
connection.close();
}
}
package com.hs.gz.hellodemo.mq.demo;
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 javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消费者
* @author hasee
*
*/
public class Consumer {
/* 默认连接用户名 */
private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
/* 默认连接密码 */
private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
/* 默认连接地址 */
private static final String BROKEURL = "tcp://139.199.158.112:61616";
public static void main(String[] args) throws Exception {
ConnectionFactory factory;
Connection connection;
Session session;
Destination destination = null;
MessageConsumer consumer;
factory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEURL);
connection = factory.createConnection();
/* 启动连接*/
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("HelloWorld");
consumer = session.createConsumer(destination);
//一直监听mqserver,如果有待消费的消息就进行消费
Message message;
while((message = consumer.receive()) != null ) {
System.out.println("consumer..." + ((TextMessage)message).getText());
}
consumer.close();
session.close();
connection.close();
}
}
标签:连接 ati you get str static 消息 create rod
原文地址:https://www.cnblogs.com/codetree/p/10051918.html