ActiveMQ(一)在linux的安装

1.安装

1. JDK环境安装

2. activeMQ安装

  • 下载mq的bin.tar.gz 包
  • 解压安装 tar -zxvf apache-activemq-5.11.1-bin.tar.gz

3. 防火墙、端口

    activemq用到两个端口:

    消息通讯的端口,默认61616

    管理控制台端口,默认8161,可在conf/jetty.xml中修改


 
 
 

 

4.启动  

    cd /opt/apache-activemq-5.11.1/bin

    ./activemq start

5. 登录 http://ip:8161   ,默认账户 admin/admin

ActiveMQ(一)在linux的安装_第1张图片

 

ActiveMQ(一)在linux的安装_第2张图片

 

2.命令行工具

 

3.java demo

生产者

import org.apache.activemq.ActiveMQConnectionFactory;
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 java.time.LocalDateTime;

/**
 * @see MyProducer
 * @since 1.0
 */
public class MyProducer {
    public static void main(String[] args) throws JMSException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://10.11.80.18: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);

        //创建消息体
        TextMessage message = session.createTextMessage("AMQ发送消息" + LocalDateTime.now().toString());

        producer.send(message);

        session.commit();
        session.close();
        connection.close();
    }     
}

 

消费者

import org.apache.activemq.ActiveMQConnectionFactory;

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.Session;
import javax.jms.TextMessage;

/**
 * @see MyConsumer
 * @since 1.0
 */
public class MyConsumer {
    public static void main(String[] args) throws JMSException {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://10.11.80.18:61616");

        //用连接工厂创建连接
        Connection connection = connectionFactory.createConnection();
        connection.start();

        //创建会话
        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);

        //创建目的地
        Destination destination = session.createQueue("my-queue");

        //创建消费者
        MessageConsumer consumer = session.createConsumer(destination);

        TextMessage message = (TextMessage) consumer.receive();
        System.out.println("接收到消息:" + message.getText());

        session.commit();
        session.close();
        connection.close();
    }
}

 

你可能感兴趣的:(消息中间件)