activeMQ安装使用
下载地址:
http://mirror.esocc.com/apache/activemq/apache-activemq/5.9.0/apache-activemq-5.9.0-bin.tar.gz
拷贝到安装路径。例如:
/usr/local/activeMQ/
解压
tar -xvf apache-activemq-5.9.0-bin.tar.gz
启动activeMQ服务
cd bin/
根据当前系统环境执行使用相应的文件进行启动服务即可。
例如:当前是Linux X86 64位操作系统
cd linux-x86-64
./activemq start
可选择option
{ console | start | stop | restart | status | dump }
启动完成后,就可以通过activeMQ自带的后台管理了。
后台地址
http://localhost:8161/hawtio/#
用户名:admin
密码:admin
接下来我们使用跑一个Active自带的事例
Publisher 实现发布
Listener 订阅
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
class Publisher extends Thread {
static String user = env("ACTIVEMQ_USER", "admin");
static String password = env("ACTIVEMQ_PASSWORD", "password");
static String host = env("ACTIVEMQ_HOST", "10.1.15.123");
static int port = Integer.parseInt(env("ACTIVEMQ_PORT", "61616"));
@Override
public void run() {
try {
String destination = "event_lss";
int messages = 10000;
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);
MessageProducer producer = session.createProducer(dest);
// 非持久化模式
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int i = 1; i <= messages; i++) {
String body = getSendMsg(i);
TextMessage msg = session.createTextMessage(body);
msg.setIntProperty("id", i);
producer.send(msg);
if ((i % 10) == 0) {
System.out.println(String.format("Sent %d messages", i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
producer.send(session.createTextMessage("SHUTDOWN"));
connection.close();
} catch (JMSException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) throws JMSException {
for (int i = 0; i < 10; i++) {
new Publisher().start();
}
}
private String getSendMsg(int i) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,ms");
return getName() + "\t" + i + "\t" + format.format(new Date()) + "abcdefghijklmnopqrstuvwxyz";
}
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;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
import javax.jms.*;
class Listener {
public static void main(String []args) throws JMSException {
String user = env("ACTIVEMQ_USER", "admin");
String password = env("ACTIVEMQ_PASSWORD", "password");
String host = env("ACTIVEMQ_HOST", "10.1.15.123");
int port = Integer.parseInt(env("ACTIVEMQ_PORT", "61616"));
String destination = arg(args, 0, "event_lss");
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);
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();
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 {
// if( count != msg.getIntProperty("id") ) {
// System.out.println("mismatch: "+count+"!="+msg.getIntProperty("id"));
// }
count = msg.getIntProperty("id");
if( count == 0 ) {
start = System.currentTimeMillis();
}
if( count % 1000 == 0 ) {
System.out.println(String.format("Received %d messages.", count));
}
System.out.println(body);
count ++;
}
} else {
System.out.println("Unexpected message type: "+msg.getClass());
}
}
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;
}
}
还没有评论,来说两句吧...