ActiveMQ那些事

默认端口

  • 8161 ActiveMQ broker
  • 61616 openwire
  • 5672 amqp
  • 61613 stomp
  • 1883 mqtt
  • 61614 ws

修改Active控制台账号密码
修改conf/jetty-realm.properties 文件,重启后生效

# username: password [,rolename ...]
admin: admin, admin
user: user, user

添加生产者消费者连接密码
默认不需要账号密码都能连得上的,编辑 conf/activemq.xml文件在节点添加下面配置:


    
    
        
    
    

注意替换成自己的用户名和密码

Spring boot 使用

Maven导入


    org.springframework.boot
    spring-boot-starter-activemq

监听生成者连接和断开状态
设备连接和断开可以监听到,可以做设备是否在线的一些操作。

@JmsListener(destination = "ActiveMQ.Advisory.Connection")
public void connectMessage(ActiveMQMessage activeMQMessage) {
    logger.info(activeMQMessage.toString());
    if (activeMQMessage.getDataStructure() instanceof ConnectionInfo) {
    ConnectionInfo connectionInfo = (ConnectionInfo) activeMQMessage.getDataStructure();
        logger.info(connectionInfo.getClientId() + " 连接");
    } else {
        logger.info("断开");
    }
}

连接getDataStructure得到的是ConnectionInfo,断开是RemoveInfo。

ActiveMQMessage连接结构示例
{
    commandId = 0, 
    responseRequired = false, 
    messageId = ID:xxx-40299-1565000371816-1:1:0:0:4222, 
    originalDestination = null, 
    originalTransactionId = null, 
    producerId = ID:xxx-40299-1565000371816-1:1:0:0, 
    destination = topic://ActiveMQ.Advisory.Connection, 
    transactionId = null, 
    expiration = 0, 
    timestamp = 0, 
    arrival = 0, 
    brokerInTime = 1565331608148, 
    brokerOutTime = 1565331608159, 
    correlationId = null, 
    replyTo = null, 
    persistent = false, 
    type = Advisory, 
    priority = 0, 
    groupID = null, 
    groupSequence = 0, 
    targetConsumerId = null, 
    compressed = false, 
    userID = null, 
    content = null, 
    marshalledProperties = org.apache.activemq.util.ByteSequence@3347e57e, 
    dataStructure = 
    ConnectionInfo {
        commandId = 0, 
        responseRequired = true, 
        connectionId = ID:xxx-40299-1565000371816-3:540, 
        clientId = xxxxx, 
        clientIp = tcp://192.168.3.23:56380, 
        userName = test, 
        password = *****, 
        brokerPath = null, 
        brokerMasterConnector = false, 
        manageable = false, 
        clientMaster = true, 
        faultTolerant = false, 
        failoverReconnect = false}, 
        redeliveryCounter = 0, size = 0, 
        properties = {
        originBrokerId=ID:xxx-40299-1565000371816-0:1, 
        originBrokerName=localhost, 
        originBrokerURL=mqtt://xxx:1883}, 
        readOnlyProperties = true, 
        readOnlyBody = true, 
        droppable = false, 
        jmsXGroupFirstForConsumer = false
    }
}
ActiveMQMessage断开结构示例
{
    commandId = 0, 
    responseRequired = false, 
    messageId = ID:xxx-40299-1565000371816-1:1:0:0:4224, 
    originalDestination = null, 
    originalTransactionId = null, 
    producerId = ID:xxx-40299-1565000371816-1:1:0:0, 
    destination = topic://ActiveMQ.Advisory.Connection, 
    transactionId = null, 
    expiration = 0, 
    timestamp = 0, 
    arrival = 0, 
    brokerInTime = 1565331608162, 
    brokerOutTime = 1565331608162, 
    correlationId = null, 
    replyTo = null, 
    persistent = false, 
    type = Advisory, 
    priority = 0, 
    groupID = null, 
    groupSequence = 0, 
    targetConsumerId = null, 
    compressed = false, 
    userID = null, 
    content = null, 
    marshalledProperties = org.apache.activemq.util.ByteSequence@33269a19, 
    dataStructure = 
    RemoveInfo {
        commandId = 0, 
        responseRequired = true, 
        objectId = ID:xxx-40299-1565000371816-3:539, 
        lastDeliveredSequenceId = -2}, 
        redeliveryCounter = 0, 
        size = 0, 
        properties = {
        originBrokerId=ID:xxx-40299-1565000371816-0:1, 
        originBrokerName=localhost, 
        originBrokerURL=mqtt://xxx:1883}, 
        readOnlyProperties = true, 
        readOnlyBody = true, 
        droppable = false, 
        jmsXGroupFirstForConsumer = false
    }
}

订阅主题接收消息

@JmsListener(destination = "UpLocation")
public void upLocation(BytesMessage bytesMessage) {

}

// 获取字符串内容代码段
try {
    byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
    bytesMessage.readBytes(bytes);
    String  msg = new String(bytes);
} catch (JMSException e) {
    logger.error(tag + " " + e.getMessage());
}

你可能感兴趣的:(mqtt,activemq)