准备
不管是本地还是云端,要先有一个activemq
安装过程
生产者
Publiser.java
package example;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTempQueue;
import org.apache.activemq.command.ActiveMQTopic;
import javax.jms.*;
class Publisher {
public static void main(String []args) throws JMSException {
String user = "admin";
String password = "admin";
String host = "127.0.0.1";
int port = Integer.parseInt("61616");
String destination = "flight_tag";
int messages = 100;
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://" + host + ":" + port);
Connection connection = factory.createConnection(user, password);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Destination dest = new ActiveMQTopic(destination);
Destination dest= new ActiveMQQueue(destination);
MessageProducer producer = session.createProducer(dest);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for( int i=1; i <= messages; i ++) {
TextMessage msg = session.createTextMessage(String.valueOf(i));
msg.setIntProperty("id", i);
producer.send(msg);
if( (i % 100) == 0) {
System.out.println(String.format("Sent %d messages", i));
}
}
// producer.send(session.createTextMessage("SHUTDOWN"));
connection.close();
}
private static String env(String key, String defaultValue) {
String rc = System.getenv(key);
if( rc== null )
return defaultValue;
return rc;
}
private static String arg(String []args, int index, String defaultValue) {
if( index < args.length )
return args[index];
else
return defaultValue;
}
}
消费者
listener.java
package example;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import javax.jms.*;
class Listener {
public static void main(String []args) throws JMSException {
String user = "admin";
String password = "admin";
String host = "127.0.0.1";
int port = Integer.parseInt("61616");
String destination = "flight_tag";
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://" + host + ":" + port);
Connection connection = factory.createConnection(user, password);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Destination dest = new ActiveMQTopic(destination);
Destination dest = new ActiveMQQueue(destination);
MessageConsumer consumer = session.createConsumer(dest);
long start = System.currentTimeMillis();
long count = 1;
System.out.println("Waiting for messages...");
while(true) {
Message msg = consumer.receive();
if( msg instanceof TextMessage ) {
String body = ((TextMessage) msg).getText();
// 在此执行方法
System.out.println(body);
if( "SHUTDOWN".equals(body)) {
long diff = System.currentTimeMillis() - start;
// System.out.println(String.format("Received %d in %.2f seconds", count, (1.0*diff/1000.0)));
break;
} else {
System.out.println("继续监听");
}
} else {
System.out.println("Unexpected message type: "+msg.getClass());
}
}
connection.close();
}
private static String arg(String []args, int index, String defaultValue) {
if( index < args.length )
return args[index];
else
return defaultValue;
}
}