- 一: linux安装mq
- 1.下载路径
- 2. 配置jdk
- 3.上传apache-activemq-5.16.0-bin.tar.gz
- 4.配置外部访问
- 5. 启动
- 6.浏览器访问
- 二: java连接mq
- 1.准备工作pom.xml
- 2.编写生产者代码
- 3.编写消费者代码
http://activemq.apache.org/ https://activemq.apache.org/components/classic/download/2. 配置jdk
[root@oracle bin]# vim /etc/profile JAVA_HOME=/opt/jdk1.8.0_221; export PATH=$JAVA_HOME/bin:$PATH3.上传apache-activemq-5.16.0-bin.tar.gz
解压tar包
tar -zxvf apache-activemq-5.16.0-bin.tar.gz -C ./mq/4.配置外部访问
cd /opt/mq/apache-activemq-5.16.0/conf [root@oracle conf]# vim jetty.xml5. 启动
cd /opt/mq/apache-activemq-5.16.0/bin/linux-x86-64 [root@oracle linux-x86-64]# ./activemq start6.浏览器访问 二: java连接mq 1.准备工作pom.xml
2.编写生产者代码4.0.0 org.example ActiveMqJmsProduce 1.0-SNAPSHOT 8 8 org.apache.activemq activemq-all 5.15.9 org.apache.xbean xbean-spring 3.16 org.slf4j slf4j-api 1.7.30 ch.qos.logback logback-classic 1.2.3 org.projectlombok lombok 1.18.16 junit junit 4.13.1
package com.yy;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;
import javax.jms.*;
import java.io.IOException;
public class JmsConsumer {
private static final String brokerURL="tcp://192.168.138.131:61616";
public static void main(String[] args) throws JMSException, IOException, InterruptedException {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(brokerURL);
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
// 事务 签收
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("first quese");
MessageConsumer consumer = session.createConsumer(destination);
// Message receive = consumer.receive();
// String name = receive.getStringProperty("name");
// System.out.println(name);
consumer.setMessageListener(x->{
if(x!=null&& x instanceof TextMessage){
String text = null;
try {
text = ((TextMessage) x).getText();
System.out.println(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
});
Thread.currentThread().join();
//System.in.read();
consumer.close();
session.close();
connection.close();
}
}
3.编写消费者代码
package com.yy;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;
import javax.jms.*;
public class JmsProduce {
private static final String brokerURL="tcp://192.168.138.131:61616";
public static void main(String[] args) throws JMSException {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(brokerURL);
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
// 事务 签收
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("first quese");
MessageProducer producer = session.createProducer(destination);
// Message msg = new ActiveMQTextMessage();
// msg.setStringProperty("name","tomcat");
for (int i = 0; i <3 ; i++) {
Message msg = session.createTextMessage(i + "msg");
producer.send(msg);
}
producer.close();
session.close();
connection.close();
}
}



