JmsListener中选择目标队列名称

JmsListener中选择目标队列名称

本文介绍了如何使Spring JMS从注解@JmsListener中选择目标队列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何帮助将不胜感激.我正在尝试使用Spring JMSListener创建MDB的替代品.我希望将目的地名称作为注释传递,但我注意到org.springframework.jms.listener.DefaultMessageListenerContainer容器配置中必须包含destinationNamedestination.我想念什么吗?如何使用目标解析器来使用@JmsListener批注中的队列名称?

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);
}}

下面是我粘贴的Xml,我现在评论了该容器,因为spring试图连接到虚拟队列而不是注释中的那个.

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>

推荐答案

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

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

这篇关于如何使Spring JMS从注解@JmsListener中选择目标队列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:58