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

如何在真实的JMS分布式体系结构中利用Spring集成?

  •  5
  • ngeek  · 技术社区  · 15 年前

    对于以下场景,我正在寻找关于最佳实践的建议和提示:

    在分布式(主要是基于Java的)系统中:

    • 许多(不同的)客户端应用程序(Web应用程序、命令行工具、RESTAPI)
    • 中央JMS消息代理(目前支持使用ActiveMQ)
    • 多个独立处理节点(在多台远程计算机上运行,计算由JMS消息负载指定的不同类型的昂贵操作)

    如何最好地应用由 Spring Integration 将客户端与工作节点分离的框架?在阅读参考文档和一些非常初步的实验时,JMS入站适配器的配置似乎天生就需要使用订阅服务器,而在分离的场景中,订阅服务器并不存在。

    小提示:应该通过JMS文本消息进行通信(为将来的可扩展性使用JSON数据结构)。

    3 回复  |  直到 15 年前
        1
  •  4
  •   Edward Dale    15 年前

    这并不能真正回答你的问题,但一定要调查 Apache Camel 用于连接不同的组件。我发现它对于将JMS队列连接到现有的Web服务非常有用,并且计划将其用于其他组件也非常有用。

    监视ActiveMQ队列中的消息、转换消息并将其发布到Web服务的示例:

    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring-2.3.0.xsd">
    
    <bean id="callbackProcessor" class="com.package.CallbackProcessor"/>
    
    <bean id="activemq" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory" ref="jmsFactory" />
    </bean>
    
    <camel:camelContext id="camel">
        <!-- Must put this in camel:endpoint because camel:from doesn't support property substitution -->
        <camel:endpoint id="callbackQueue" uri="activemq:queue:${jms.callback-queue-name}"/>
        <camel:route>
            <camel:from ref="callbackQueue"/>
            <camel:process ref="callbackProcessor"/>
            <camel:to uri="http://dummy"/><!-- This will be replaced by the callbackProcessor with the callback URL in the message -->
        </camel:route>
    </camel:camelContext>
    </beans>
    

    这就是我们的Spring应用程序中启动camel并开始处理消息所必需的全部内容。

        2
  •  3
  •   ngeek    15 年前

    这是我今天提出的Spring集成,如果您发现可以改进的地方,请跟进。

    在客户端,可以通过simplemessaginggateway发送和接收消息:

    <?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:context="http://www.springframework.org/schema/context"   
        xmlns:integration="http://www.springframework.org/schema/integration"
        xmlns:jms="http://www.springframework.org/schema/integration/jms"   
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/integration/jms
                http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
                http://www.springframework.org/schema/integration
                http://www.springframework.org/schema/integration/spring-integration.xsd">
    
        <import resource="integration-common.xml"/>
    
        <!-- Communication Gateway for the Client (send/receive) -->
        <bean id="gateway" class="org.springframework.integration.gateway.SimpleMessagingGateway">
            <property name="requestChannel" ref="SenderChannel"/>
            <property name="replyChannel" ref="InboundChannel"/>
            <property name="replyTimeout" value="1000"/>
        </bean><!-- TODO: could use integration:gateway -->
    
        <!-- Sending out message to JMS request queue -->
        <integration:channel id="SenderChannel"/>
        <jms:outbound-channel-adapter
                            channel="SenderChannel" 
                            destination="requestQueue" />
    
        <!-- Listen to incoming messages on JMS reply queue -->
        <integration:channel id="InboundChannel">
            <integration:queue/>
        </integration:channel>
        <jms:message-driven-channel-adapter
                destination="replyQueue"
                channel="InboundChannel" />
    
    </beans>
    

    处理节点端的配置如下(有关Spring集成元素的更多说明,请参阅内联注释):

    <?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:context="http://www.springframework.org/schema/context"   
        xmlns:integration="http://www.springframework.org/schema/integration"
        xmlns:jms="http://www.springframework.org/schema/integration/jms"   
        xmlns:stream="http://www.springframework.org/schema/integration/stream" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/integration/jms
                http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
                http://www.springframework.org/schema/integration
                http://www.springframework.org/schema/integration/spring-integration.xsd">
    
        <import resource="integration-common.xml"/>
    
        <!-- Read in Message Endpoint Service Activator classes --> 
        <context:component-scan base-package="sample.integration.jmsbasic"/>
    
        <!-- Listen to incoming messages on the JMS request queue -->
        <integration:channel id="jmsinToProcChannel"/>
        <jms:message-driven-channel-adapter
                destination="requestQueue"
                channel="jmsinToProcChannel"/>
    
        <!-- Delegate message to service implementation and take care of answer -->
        <integration:service-activator 
                input-channel="jmsinToProcChannel" 
                ref="procService"
                output-channel="jmsBackChannel" />
    
        <!-- Send answer back to JMS reply queue -->
        <integration:channel id="jmsBackChannel"/>
        <jms:outbound-channel-adapter
                            channel="jmsBackChannel" 
                            destination="replyQueue" />
    
    </beans>
    
        3
  •  1
  •   Paul McKenzie    15 年前

    您是在问是否可以使用Spring集成来实现协议吗? bridge ?那么答案是肯定的,而且非常简单。