目录
一、创建客户端
二、创建接收端
三、发布
四、用户订阅
五、配置安全认证
public class Sender {
public static void main(String[] args) throws Exception{
//第一步:建立connectionFactory工厂对象【需填入用户名、密码、要连接的地址】
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("bhz","bhz","tcp://localhost:61616");
//第二步:通过ConnectionFactory工厂对象创建一个Connection连接,并且调用Connection的start方法开启连接【connection默认是关闭的】
Connection connection = connectionFactory.createConnection();
connection.start();
//第三步:通过connection创建session会话(上下文环境对象),用于接收消息,参数配置1为是否启用事务,参数配置2为签收模式【一般我们设置为自动签收】
// Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//使用事务的方式进行消息的发送
// Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//使用CLIENT端签收的方式
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
//第四步:通过session创建Destination对象,指的是一个客户端用来指定生产消息目标和消费消息来源的对象,在PTP模式中,Destination被称作Queue即队列;在Pub/Sub模式,Destination被称作Topic即主题.在程序中可以使用多个Queue和Topic.
Destination destination = session.createQueue("queue1");
//第五步:通过session对象创建消息的发送和接收对象(生产者和消费者)MessageProducer/MessageConsumer
MessageProducer messageProducer = session.createProducer(null);
//第六步:可以使用MessageProducer的setDeliveryMode方法为其设置持久化特性和非持久化特性(DeliveryMode)
messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
//第七步:使用JMS规范的TextMessage形式创建数据(通过session对象),并用MessageProducer的send方法发送数据
for (int i = 1; i <= 5; i++) {
TextMessage textMessage = session.createTextMessage("helloworld"+i);
//第一个参数: 目的地
//第二个参数: 消息
//第三个参数: 是否持久化
//第四个参数: 优先级【0-9 0-4表示普通 5-9表示加急 默认4】
//第五个参数: 消息在mq上的存放有效期【单位毫秒】
messageProducer.send(destination, textMessage, DeliveryMode.NON_PERSISTENT, i, 1000*60*2);
System.out.println("生产者:"+textMessage.getText());
}
//使用事务提交
//session.commit();
if (connection!=null) {
connection.close();
}
}
}
public class Receiver {
public static void main(String[] args) throws Exception{
//第一步:建立connectionFactory工厂对象【需填入用户名、密码、要连接的地址】
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("bhz","bhz","tcp://localhost:61616");
//第二步:通过ConnectionFactory工厂对象创建一个Connection连接,并且调用Connection的start方法开启连接【connection默认是关闭的】
Connection connection = connectionFactory.createConnection();
connection.start();
//第三步:通过connection创建session会话(上下文环境对象),用于接收消息,参数配置1为是否启用事务,参数配置2为签收模式【一般我们设置为自动签收】
Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
//第四步:通过session创建Destination对象,指的是一个客户端用来指定生产消息目标和消费消息来源的对象,在PTP模式中,Destination被称作Queue即队列;在Pub/Sub模式,Destination被称作Topic即主题.在程序中可以使用多个Queue和Topic.
Destination destination = session.createQueue("queue1");
//第五步:通过session对象创建消息的发送和接收对象(生产者和消费者)MessageProducer/MessageConsumer
MessageConsumer messageConsumer = session.createConsumer(destination);
//第七步:使用JMS规范的TextMessage形式创建数据(通过session对象),并用MessageProducer的send方法发送数据
while (true) {
TextMessage msg = (TextMessage) messageConsumer.receive();
//手动去签收消息,另起一个线程(TCP)去通知我们的MQ服务,确认签收
Thread.sleep(1000);
msg.acknowledge();
if(msg == null) break;
System.out.println(msg.getText());
}
if (connection!=null) {
connection.close();
}
}
}
public class Publish {
private ConnectionFactory factory;
private Connection connection;
private Session session;
private MessageProducer producer;
public Publish(){
try {
factory = new ActiveMQConnectionFactory(
"bhz",//ActiveMQConnection.DEFAULT_USER,
"bhz",//ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(null);
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendMessage() throws Exception{
Destination destination = session.createTopic("topic1");
TextMessage textMessage = session.createTextMessage("我是内容");
producer.send(destination, textMessage);
}
public static void main(String[] args) throws Exception {
Publish p = new Publish();
p.sendMessage();
}
}
public class Consumer1 {
private ConnectionFactory factory;
private Connection connection;
private Session session;
private MessageConsumer consumer;
public Consumer1(){
try {
factory = new ActiveMQConnectionFactory(
"bhz",//ActiveMQConnection.DEFAULT_USER,
"bhz",//ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
} catch (Exception e) {
e.printStackTrace();
}
}
public void receive() throws Exception{
Destination destination = session.createTopic("topic1");
consumer = session.createConsumer(destination);
consumer.setMessageListener(new Listener());
}
class Listener implements MessageListener{
public void onMessage(Message message) {
try {
System.out.println("consumer1收到消息:"+((TextMessage)message).getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
Consumer1 p = new Consumer1();
p.receive();
}
}
1、 activemq.xml中
在
2、对应的java代码要改成
//第一步:建立connectionFactory工厂对象【需填入用户名、密码、要连接的地址】
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("bhz","bhz","tcp://localhost:61616");
3、页面登录:http://127.0.0.1:8161/ 还是按照jconf/jetty-real.properties中的配置来
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin