How to make Spring JMS pick destination queue name from annotation @JmsListener

微笑、不失礼 提交于 2019-12-24 02:22:44

问题


Any help will be greatly appreciated. I am trying to create a replacement for MDB with spring JMSListener. I wanted the destination name to be passed as a annotation, but i noticed that org.springframework.jms.listener.DefaultMessageListenerContainer container config has destinationName or destination as mandatory. Am i missing something ? How to make use of destination resolver to use queue name from @JmsListener annotation?

@Component 
public class InitStRspnLstnr {

@JmsListener(destination = "${xyz.company.MQ.INITST.RSPN.FADS.Q}")
public void onMessage(Message msg) throws JMSException {
    System.out.println("**********************************************"+msg);
}}

Below is my Xml pasted, I commented the container for now, since spring was trying to connect to dummy queue instead of the one in annotation..

        <?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:util="http://www.springframework.org/schema/util"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:cxf="http://cxf.apache.org/core"    
        xmlns:jaxrs="http://cxf.apache.org/jaxrs"
        xmlns:jaxws="http://cxf.apache.org/jaxws"    
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd  
            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
            http://cxf.apache.org/jaxrs
            http://cxf.apache.org/schemas/jaxrs.xsd
            http://cxf.apache.org/jaxws
            http://cxf.apache.org/schemas/jaxws.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
            ">

        <import resource="classpath*:com/xyz/svc/component.xml"/>

        <mvc:annotation-driven enable-matrix-variables="true"/>
        <context:component-scan base-package="com.xyz.css.rplr.initst"></context:component-scan>

        <!-- WebSphere MQ Connection setup start -->
        <bean id="mqIdsConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
            <property name="hostName"><value>${xyz.company.MQ.HOST.NM}</value></property>
            <property name="port"><value>${xyz.company.MQ.PORT}</value></property>
            <property name="queueManager"><value>${xyz.company.MQ.QMNGR}</value></property>
            <property name="channel"><value>${xyz.company.MQ.CHANNEL}</value></property>
            <property name="CCSID"><value>819</value></property>
            <property name="transportType">
                <util:constant static-field="com.ibm.mq.jms.JMSC.MQJMS_TP_CLIENT_MQ_TCPIP" />
            </property>
        </bean>
        <bean id="jmsQueueIdsConnectionFactory"
            class="org.springframework.jms.connection.SingleConnectionFactory">
            <property name="targetConnectionFactory">
                <ref bean="mqIdsConnectionFactory" />
            </property>
        </bean>
        <bean id="jmsDestinationResolver"
            class="org.springframework.jms.support.destination.DynamicDestinationResolver">
        </bean>
        <bean id="jmsQueueIdsTemplate" class="org.springframework.jms.core.JmsTemplate">
            <property name="connectionFactory">
                <ref bean="jmsQueueIdsConnectionFactory" />
            </property>
            <property name="destinationResolver">
                <ref bean="jmsDestinationResolver" />
            </property> <!--    
            <property name="defaultDestinationName">
                <value>${xyz.company.MQ.INITST.Q}</value>
            </property>-->
            <property name="pubSubDomain">
                <value>false</value>
            </property>
            <property name="receiveTimeout">
                <value>20000</value>
            </property>
        </bean>

        <!-- <bean id="jmsContainer"
            class="org.springframework.jms.listener.DefaultMessageListenerContainer">
            <property name="connectionFactory">
                <ref bean="jmsQueueIdsConnectionFactory" />
            </property>
            <property name="destinationResolver">
                <ref bean="jmsDestinationResolver" />
            </property>
            <property name="destinationName" value="dummyQueue"/>
            <property name="concurrency" value="3-10" />
        </bean> -->

        <!-- WebSphere MQ Connection setup end -->
    </beans>

回答1:


Added working version below. defaultDestinationName is not needed in xml , if its set up in code via annotation.. ${company.project.MQ.module.Q} and other values are loaded from properties file, it can be hard coded too.

@Component 
public class SampleLstner{     
@JmsListener(concurrency = "1", destination = "${company.project.MQ.module.Q}", containerFactory = "jmsListenerContainerFactory")
           public void onMessage(Message msg) throws JMSException {}
 }

XML

<?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:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cxf="http://cxf.apache.org/core"    
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:jaxws="http://cxf.apache.org/jaxws"    
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
    http://cxf.apache.org/jaxrs
    http://cxf.apache.org/schemas/jaxrs.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
    http://www.springframework.org/schema/jms
    http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
    ">

<import resource="classpath*:com/company/svc/component.xml"/>

<mvc:annotation-driven enable-matrix-variables="true"/>
<context:component-scan base-package="com.company.test.main.module"></context:component-scan>

<!-- WebSphere MQ Connection setup start -->
<bean id="mqIdsConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
    <property name="hostName"><value>${company.project.MQ.HOST.NM}</value></property>
    <property name="port"><value>${company.project.MQ.PORT}</value></property>
    <property name="queueManager"><value>${company.project.MQ.QMNGR}</value></property>
    <property name="channel"><value>${company.project.MQ.CHANNEL}</value></property>
    <property name="CCSID"><value>819</value></property>
    <property name="transportType"><value>1</value></property>
</bean>
<bean id="jmsQueueIdsConnectionFactory"
    class="org.springframework.jms.connection.SingleConnectionFactory">
    <property name="targetConnectionFactory">
        <ref bean="mqIdsConnectionFactory" />
    </property>
</bean>
<bean id="jmsDestinationResolver"
    class="org.springframework.jms.support.destination.DynamicDestinationResolver">
</bean>
<bean id="jmsQueueIdsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory">
        <ref bean="jmsQueueIdsConnectionFactory" />
    </property>
    <property name="destinationResolver">
        <ref bean="jmsDestinationResolver" />
    </property> <!--    
    <property name="defaultDestinationName">
        <value>${company.project.MQ.module.Q}</value>
    </property>-->
    <property name="pubSubDomain">
        <value>false</value>
    </property>
    <property name="receiveTimeout">
        <value>20000</value>
    </property>
</bean>

<bean id="jmsListenerContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
    <property name="connectionFactory">
        <ref bean="jmsQueueIdsConnectionFactory" />
    </property>
    <property name="destinationResolver">
        <ref bean="jmsDestinationResolver" />
    </property>
    <property name="concurrency" value="1" />
 </bean>
<!-- WebSphere MQ Connection setup end -->



来源:https://stackoverflow.com/questions/46434130/how-to-make-spring-jms-pick-destination-queue-name-from-annotation-jmslistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!