docker安装activeMq
这里使用Docker安装
- 查询Docker镜像:
docker search activemq
- 下载Docker镜像:
docker pull webcenter/activemq
- 创建&运行ActiveMQ容器:
docker run -d --name myactivemq -p 61616:61616 -p 8161:8161 -p 61613:61613webcenter/activemq
61616是 activemq 的容器使用端口(映射为61616),8161是 web 页面管理端口(对外映射为8161)
61613是 activemq 的容器使用stomp端口(映射为61613)
mq账号密码的修改
1.首先找到容器
docker ps
2.进入docker容器内
docker exec -it 容器id bash
3.搜索文件,并找到
find / -name 'jetty-realm.properties'
4.修改
vi jetty-realm.properties
网页访问
http://192.168.2.1:8161/admin/sendMessage.action
admin admin
其他服务连接配置用户名密码
配置连接密码
编辑activemq.xml文件,放置到 shutdownHooks 下方即可。
<!-- 添加访问ActiveMQ的账号密码 -->
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users,admins"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
修改conf中credentials.properties文件进行密码设置:
activemq.username=admin
activemq.password=123456
guest.password=123456
python 连接示例
# coding:utf-8
import time
import stomp
import json
listener_name = 'SampleListener'
class SampleListener(object):
def on_error(self, headers, message):
print('received an error "%s"' % message)
def on_message(self, headers, message):
print('message: %s' % message)
#推送到主题
def send_to_topic(host,port,username,password,topic,msg):
conn = stomp.Connection([(host, int(port))], auto_content_length=False)
conn.set_listener(listener_name, SampleListener())
conn.start()
conn.connect(username=username, passcode=password)
#msg = json.dumps(msg, ensure_ascii=False)
conn.send(topic, body=msg)
print("send success,msg={}".format(msg))
conn.disconnect()
##从主题接收消息
def receive_from_topic(host,port,username,password,topic):
conn = stomp.Connection([(host, int(port))], auto_content_length=False)
conn.set_listener(listener_name, SampleListener())
conn.start()
conn.connect(username=username, passcode=password)
conn.subscribe(topic, id="seemmo", ack="auto")
i = 1
while 1:
print(i)
elements = list()
elements.append("1.0")
elements.append(str(i))
elements.append("0")
structure_data = ",".join(elements)
send_to_topic(host,port,username,password,topic,structure_data)
time.sleep(1) # secs
i = i+1
time.sleep(3) # secs
conn.disconnect()
if __name__ == "__main__":
host = "192.168.2.1"
port = "61613"
username='admin'
password='123456'
topic="/topic/AI"
elements = list()
elements.append("1.0")
elements.append("start")
elements.append("1")
structure_data = ",".join(elements)
#send_to_topic(host,port,username,password,topic,structure_data)
receive_from_topic(host,port,username,password,topic)
java 连接示例
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
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;
public class AppProducer {
public static final String url = "tcp://192.168.2.1:61616";
public static final String topicName = "AI";
public static void main(String[] args) throws JMSException{
//1. 创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( url);
//2. 创建Connection
Connection connection = connectionFactory.createConnection();
//3. 启动链接
connection.start();
//4. 创建会话
Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE);
//5. 创建一个目标
Destination destination = session.createTopic( topicName);
//6. 创建一个生产者
MessageProducer producer = session.createProducer( destination);
for( int i=0; i<100; i++){
//7. 创建消息
TextMessage textMessage = session.createTextMessage( "test" + i);
//8. 发布消息
producer.send( textMessage);
System.out.println( "发送消息" + textMessage.getText());
}
//9. 关闭链接
connection.close();
}
}
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class AMQConsumer {
public static final String url = "tcp://192.168.2.92:61616";
public static final String topicName = "HIATMP.HISENSE.ILLEGAL.AI";
public static void main(String[] args) throws JMSException{
//1. 创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( url);
//2. 创建Connection
Connection connection = connectionFactory.createConnection();
//3. 启动链接
connection.start();
//4. 创建会话
Session session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE);
//5. 创建一个目标
Destination destination = session.createTopic(topicName);
//6. 创建一个消费者
MessageConsumer consumer = session.createConsumer( destination);
//7. 创建一个监听器
consumer.setMessageListener( new MessageListener() {
@Override
public void onMessage(Message message) {
TextMessage textMessage = ( TextMessage) message;
try{
System.out.println( "0接收消息" + textMessage.getText());
}catch( JMSException e){
e.printStackTrace();
}
}
});
//8. 关闭链接
//connection.close();
}
}
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
