Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在真实的JMS分布式体系结构中利用Spring集成?_Java_Spring_Jms_Spring Integration - Fatal编程技术网

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

Java 如何在真实的JMS分布式体系结构中利用Spring集成?,java,spring,jms,spring-integration,Java,Spring,Jms,Spring Integration,对于以下场景,我正在寻求您关于最佳实践的建议和提示: 在分布式(主要基于Java)系统中,具有: 许多(不同的)客户端应用程序(web应用程序、命令行工具、REST API) 中央JMS消息代理(目前支持使用ActiveMQ) 多个独立的处理节点(在多台远程机器上运行,计算JMS消息负载指定的不同类型的昂贵操作) 如何最好地应用框架提供的JMS支持来将客户机与工作节点解耦?在阅读参考文档和一些最初的实验时,JMS入站适配器的配置似乎天生就需要使用订阅者,而在解耦场景中,订阅者是不存在的 小提

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

在分布式(主要基于Java)系统中,具有:

  • 许多(不同的)客户端应用程序(web应用程序、命令行工具、REST API)
  • 中央JMS消息代理(目前支持使用ActiveMQ)
  • 多个独立的处理节点(在多台远程机器上运行,计算JMS消息负载指定的不同类型的昂贵操作)
如何最好地应用框架提供的JMS支持来将客户机与工作节点解耦?在阅读参考文档和一些最初的实验时,JMS入站适配器的配置似乎天生就需要使用订阅者,而在解耦场景中,订阅者是不存在的


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

这并不能真正回答您的问题,但请确保您仔细研究如何连接不同的组件。我发现它对于将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并开始处理消息所需的全部内容。

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

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

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

<?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集成元素的更多解释,请参阅内联注释):



谢谢,Camel当然也是一个选项,但是这不需要更多的配置开销吗?或者这是可行的吗?我认为配置开销不会太大。我用一个例子更新了我的答案。没错,这看起来确实很简洁。感谢您发布更多详细信息。我不确定这是否能解决我的问题:一方面,客户端应用程序将配置为使用入站通道适配器将消息提交到JMS队列中,在处理节点上,将配置为使用出站适配器拾取并处理它。客户端应用程序在很大程度上是不变的,除了使用他们选择的协议(WS、REST等)与桥接应用程序通信。桥接应用程序会将所有这些请求转换为JMS请求,您的工作节点将在竞争消费者模式上使用这些请求。在最佳情况下,客户端不必了解任何有关协议的信息。您能否详细说明如何在所描述的场景中最好地利用桥接模式?谢谢
<?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>