Active MQ
1.我们现在把 tar包 并拖拽到虚拟机中
apache-activemq-5.9.0-tar.gz
2.解压 文件
tar -zxvf 文件名
3.进入该文件目录 的bin下面
4.启动该服务
./activemq start
5.查看是否已经启动
netstat -a |grep 61616
如果现实 监听状态 则为启动
6.我们可以在我们 的windows上面的浏览器输入地址
http://192.168.1.31:8161/admin/
ip是你的 虚拟机的 id 如果有静态ip可以用静态ip
8161是固定的端口号
后面的admin也是固定的
但是这步 可能出现连接不上的问题
那有可能是你的虚拟机的防火墙没有关闭的原因
我们关闭虚拟机的防火墙 然后在执行
然后会出现一个 弹框 上面让你输入 用户名和密码
用户名 密码 默认都是 admin
你填写提交后 我们会登录到这个页面
这个代表你已经成功了
接下来代码的部分
我们创建一个 maven的项目
首先我们引入节点
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>3.16</version>
</dependency>
然后我们写 代码:
首先是我们的发送请求的 类:
package cn.happy.send;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* Created by linlin on 2017/11/26.
*/
public class JmsSend {
public static void main(String[] args) throws JMSException, InterruptedException {
ConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("tcp://192.168.1.31:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session=connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue");
MessageProducer producer = session.createProducer(destination);
for(int i=0; i<3; i++) {
TextMessage message = session.createTextMessage("message--"+i);
Thread.sleep(1000);
//通过消息生产者发出消息
producer.send(message);
}
session.commit();
session.close();
connection.close();
}
}
然后是接收的 代码:
package cn.happy.Receiver;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* Created by linlin on 2017/11/26.
*/
public class JmsReceiver {
public static void main(String[] args) throws Exception {
ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://192.168.1.31:61616");
Connection connection = cf.createConnection();
connection.start();
final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue");
MessageConsumer consumer = session.createConsumer(destination);
int i=0;
while(i<3) {
i++;
TextMessage message = (TextMessage) consumer.receive();
session.commit();
System.out.println("收到消息:" + message.getText());
}
session.close();
connection.close();
}
}
当我们执行完接收数据后 我们的数据会有发生改变
如果结果一样 ,配成功了!