分布式服务框架学习笔记9 ActiveMQ入门3 管理、生产者/消费者模式
一、管理界面
ActiveMQ安装以后,通过浏览器访问:
http://localhost:8161/admin/
输入账号admin 密码admin
即可打开管理界面。
修改密码,打开 conf/jetty.xml文件,找到
<bean id="securityConstraint" class="org.eclipse.jetty.http.security.Constraint">
<property name="name" value="BASIC" />
<property name="roles" value="admin" />
<property name="authenticate" value="false" />
</bean>
将property name为authenticate的属性value=“false” 改为"true"
二、然后打开conf/jetty-realm.properties
admin: admin, admin
用户名和密码的格式是:
用户名 : 密码 ,角色名
三、生产者/消费者模式,消费监听代码
代码结构:
App.java
package com.whrsmart.ActiveMQ;
import javax.jms.JMSException;
import org.apache.activemq.ActiveMQConnection;
/**
*
*/
public class App
{
public static void main( String[] args )
{
String username = "admin1";
String password = "admin1";
String url = ActiveMQConnection.DEFAULT_BROKER_URL;
// 创建生产者
Producer producer;
try {
producer = new Producer(url, username, password);
// 生产者产生一条消息
producer.sendMessage("Hello World");
producer.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
ConsumerMain.java
package com.whrsmart.ActiveMQ;
public class ConsumerMain {
public static void main(String[] args) {
ConsumeTools tools = new ConsumeTools();
try {
tools.consumeMessage();
} catch (Exception e) {
e.printStackTrace();
}
}
}
ConsumeTools
package com.whrsmart.ActiveMQ;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ConsumeTools implements MessageListener {
private String user ="admin1";
private String password ="admin1";
private String url = ActiveMQConnection.DEFAULT_BROKER_URL;// "tcp://127.0.0.1:61616";
private String subject = "HELLO";
private Destination destination = null;
private Connection connection = null;
private Session session = null;
private MessageConsumer consumer = null;
// 初始化
private void initialize() throws JMSException, Exception {
// 连接工厂是用户创建连接的对象,这里使用的是ActiveMQ的ActiveMQConnectionFactory根据url,username和password创建连接工厂。
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
user, password, url);
// 连接工厂创建一个jms connection
connection = connectionFactory.createConnection();
// 是生产和消费的一个单线程上下文。会话用于创建消息的生产者,消费者和消息。会话提供了一个事务性的上下文。
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 不支持事务
// 目的地是客户用来指定他生产消息的目标还有他消费消息的来源的对象,两种消息传递方式:点对点和发布/订阅
destination = session.createQueue(subject);
// 会话创建消息的生产者将消息发送到目的地
consumer = session.createConsumer(destination);
}
// 消费消息
public void consumeMessage() throws JMSException, Exception {
initialize();
connection.start();
// 开始监听
consumer.setMessageListener(this);
}
// 关闭连接
public void close() throws JMSException {
if (consumer != null)
consumer.close();
if (session != null)
session.close();
if (connection != null)
connection.close();
}
// 消息处理函数
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String msg = txtMsg.getText();
if(msg.equals("1")){
new Thread().start();//耗时5分钟
}else{
new Thread().start();//耗时10秒钟
}
System.out.println("Consumer:->Received: " + msg);
} else {
System.out.println("Consumer:->Received: " + message);
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}