代码之家  ›  专栏  ›  技术社区  ›  Jugunu

如何使Spring JMS从注释@JmsListener中选取目标队列名称

  •  2
  • Jugunu  · 技术社区  · 7 年前

    任何帮助都将不胜感激。我正试图用spring JMSListener创建MDB的替代品。我希望目标名称作为注释传递,但我注意到 org.springframework.jms.listener.DefaultMessageListenerContainer 容器配置有 destinationName destination @JmsListener 注释?

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

            <?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 回复  |  直到 7 年前
        1
  •  2
  •   Saad    7 年前

    在下面添加了工作版本。如果通过注释在代码中设置defaultDestinationName,则xml中不需要它。。 ${company.project.MQ.module.Q} 其他值从属性文件加载,也可以硬编码。

    @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 -->