【问题标题】:spring integration with activemqspring 与 activemq 的集成
【发布时间】:2016-05-08 08:25:30
【问题描述】:

我使用 spring 大约一年了,在我遇到 spring 集成之前,一切都非常明显和简单。很惭愧,我无法创建出站网关来将消息发送到远程 JMS (ActiveMQ) 通道。在集成之前我只是直接使用了JmsTemplates和@JmsListeners,完全没有问题。

  1. request-destination 和 request-channel 有什么区别?
  2. 这是我的配置:

        @Configuration
        @EnableJms
        public class JmsConfig {
         ...
    
    
            @Value("${activemq.url}")
            private String brokerUrl;
    
    
            @Bean
            public JmsTemplate jmsTemplate() {
                JmsTemplate jmsTemplate = new JmsTemplate(queueConnectionFactory());
                jmsTemplate.setDefaultDestinationName(bookingChannelName);
                return jmsTemplate;
            }
            /*
            @Bean(name = "bookingChannel")
            public Queue activeMQQueue() {
                return new ActiveMQQueue(bookingChannelName);
            }
            */            
    
    
            @Bean
            public JmsListenerContainerFactory jmsListenerContainerFactory() {
                SimpleJmsListenerContainerFactory listenerFactory = new SimpleJmsListenerContainerFactory();
                listenerFactory.setConnectionFactory(queueConnectionFactory());
                return listenerFactory;
            }
            ...
    

    }

    还有一个 xml 文件,我在根 beans 元素中得到的所有内容是:

    <jms:outbound-gateway request-destination-name="reqDestination" request-channel="bookingChannel" />
    

    然后,有一个网关代码:

    @MessagingGateway
    public interface BookingGateway<T> {
    
        @Gateway
        void bookTicket(T ticket);
    }
    

    最后,这是我使用网关的方法:

    @Component
    public class BookingGatewayImpl<T> {
    
    
        @Autowired
        private BookingGateway bookingGateway;
    
        public <U> void bookTicket(T ticket, BiConsumer<T, U> onStatusReceived) {
            bookingGateway.bookTicket(ticket); // second param is not utilized yet
        }
    }
    

当机票即将被预订时,我得到:

send is not supported, because no request channel has been configured

另外,我无法从第一个清单中取消注释 ActiveMQQueue bean,因为 spring 说它与 MessageChannel 不兼容:

Bean named 'bookingChannel' must be of type [org.springframework.messaging.MessageChannel], but was actually of type [org.apache.activemq.command.ActiveMQQueue]

为什么目的地应该是MessageChannel 类型?我到底做错了什么以及如何发送此票消息?

【问题讨论】:

    标签: java spring jms activemq spring-integration


    【解决方案1】:

    bookingChannel 是您的 MessagingGateway 和 jms 网关之间的 MessageChannel。目的地是 AMQP 队列。

    使用

    @Gateway(requestChannel="bookingChannel")
    
    @Bean(name = "bookingDestination")
    public Queue activeMQQueue() {
        return new ActiveMQQueue(bookingChannelName);
    }
    
    @Bean(name = "bookingChannel")
    public MessageChannel bookingChannel() {
        return new DirectChannel();
    }
    
    <jms:outbound-gateway request-destination-name="bookingDestination" request-channel="bookingChannel" />
    

    【讨论】:

    • &lt;jms:outbound-gateway request-destination-name="bookingDestination" request-channel="bookingChannel" /&gt; - request-destination 我猜。非常感谢。
    【解决方案2】:

    最后,想出了以下配置。几乎明白了它现在的工作方式:

    <?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:int="http://www.springframework.org/schema/integration"
           xmlns:jms="http://www.springframework.org/schema/integration/jms"
           xmlns:id="http://www.springframework.org/schema/integration"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/integration
                http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">
    
        <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="nio://127.0.0.1:61616"/>
        </bean>
    
        <bean id="requestQueue" class="org.apache.activemq.command.ActiveMQQueue">
            <constructor-arg value="bookingChannel"/>
        </bean>
    
        <id:channel id="requestChannel"/>
    
        <bean id="channelInterceptor" class="c.e.mentoring.integration.gateway.RequestChannelInterceptor"/>
        <jms:outbound-channel-adapter channel="requestChannel" destination="requestQueue"/>
        <int:channel-interceptor ref="channelInterceptor"/>
    </beans>
    

    【讨论】:

      猜你喜欢
      • 2011-02-23
      • 2014-08-23
      • 2014-10-03
      • 2017-12-12
      • 2017-05-24
      • 2015-12-04
      • 2020-11-17
      • 2021-05-14
      • 2018-09-13
      相关资源
      最近更新 更多