Article From:https://www.cnblogs.com/xumz/p/9558914.html

pom

        <dependency>  
        <groupId>org.apache.activemq</groupId>  
            <artifactId>activemq-core</artifactId>
            <version>5.6.0</version>
        </dependency>  
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>5.11.0</version>
        </dependency>    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>    

spring-message

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
    default-lazy-init="true">


    <!-- ActiveMQ The ConnectionFactory that really generates Connection is provided by the corresponding JMS service provider.-->
    <bean id="activeMqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://100.100.100.100:61616" />
        <property name="userName" value="admin"></property>
        <property name="password" value="admin"></property>
    </bean>
    <!-- Configure spring to manage connectionFactory-->
    <bean id="connectionFactory"
        class="org.springframework.jms.connection.CachingConnectionFactory">
        <constructor-arg ref="activeMqConnectionFactory" />
        <property name="sessionCacheSize" value="100" />
    </bean>
    <!--This is the destination (topic), one to many.-->
    <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="Xiamen Yi Lianzhong " />
    </bean>

    <!-- Message monitor class, write it yourself.-->
    <bean id="messageListener"
        class="com.xxx.MessageListener" />
    <!-- MessageListener-->
    <bean
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"></property>
        <property name="destination" ref="topicDestination"></property>
        <property name="messageListener" ref="MessageListener"></property>
    </bean>
</beans>

javaclass

package com.xxx;

import javax.jms.BytesMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.mortbay.log.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PushMsgMessageListener implements MessageListener {
    private Logger logger = LoggerFactory.getLogger(MessageListener.class);
    
    @Autowired
    private IParkPassRecordService parkPassRecordService;
    
    @Override
    public void onMessage(Message message) {
        BytesMessage  bytesMessage = (BytesMessage ) message;
        logger.info("=======Push the message to receive from the message queue = = = = = = = = = = = = = = = ".Try {Byte [] data=new byte[(int) bytesMessagE.getBodyLength ()];BytesMessage.readBytes (data);} catCH (Exception E) {Logger.error ("processing failure, {}", e.getMessage ());}}}

 

Leave a Reply

Your email address will not be published. Required fields are marked *