【问题标题】:JMS & Spring Integration, removing the dependency on having a ActiveMQ instanceJMS 和 Spring 集成,消除对拥有 ActiveMQ 实例的依赖
【发布时间】:2023-03-03 08:10:24
【问题描述】:

我最近设置了一些到 AcitveMQ 的日志记录作为一个“一劳永逸”的服务,这样应用程序就可以只向“ActivityLoggingChannel”发送一条消息,而不必处理日志记录的横切关注点。

所有内容都发送到 ActivityLoggingGateway,它只是一个带有默认通道的接口。然后,这会向 Pojo(动态路由器)查询通道名称以获取消息端点。动态路由器有一个 JMX 入口点,它允许我动态切换端点。如果消息端点设置为jmsChannelSender,无法解析ActiveMQ url,会导致整个系统崩溃。

我遇到的问题是,如果无法访问 ActiveMQ URL,我希望系统恢复到使用简单的多线程进程方法的不同消息通道。

下面是spring集成配置。使用版本 2.0.0.RELEASE

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:jms="http://www.springframework.org/schema/integration/jms"
    xmlns:int-jmx="http://www.springframework.org/schema/integration/jmx"
    xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
        http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx-2.0.xsd
        http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms-2.0.xsd
        http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream-2.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <import resource="dm-activitylogging-services.xml"/>

    <bean name="decisionActivityLoggingAspect" class="com.idna.dm.aspects.logging.activity.DecisionActivityLogAspect" 
        factory-method="aspectOf">
        <property name="activityLoggingGateway">
            <ref bean="activityLoggingGateway" />
        </property>
    </bean>

<!-- New Activity Logging Services Reference dm-activity-logging -->    
<!-- JMS Channel Adapter -->
    <int:channel id="jmsSenderChannel" />

    <bean id="destinationLocalQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="${activemq.queuename.activitylogging}" />
    </bean>

    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="${activemq.url}" />
    </bean>

    <bean id="activityLogConverter" class="com.idna.dm.domain.activitylogging.jms.ActivityLogConverter" />
    <jms:outbound-channel-adapter channel="jmsSenderChannel"  destination="destinationLocalQueue" connection-factory="connectionFactory" message-converter="activityLogConverter"/>

<!--  In Process Adapter -->

    <int:channel id="inProcessChannel" />   
    <int:outbound-channel-adapter channel="inProcessChannel" ref="inProcessAdapter" method="persistLog" />
    <bean id="inProcessAdapter" class="com.idna.dm.logging.activity.impl.InProcessActivityLoggingImpl" >
        <property name="activityLoggingService" >
            <ref bean="activityLogging" />
        </property>
    </bean>

    <int:channel id="asyncInProcessChannel" />
    <int:outbound-channel-adapter channel="asyncInProcessChannel" ref="asyncInProcessAdapter" method="persistLog" />
    <bean id="asyncInProcessAdapter" class="com.idna.dm.logging.activity.impl.AsyncInProcessActivityLoggingImpl" >
        <property name="activityLoggingService">
            <ref bean="activityLogging" />
        </property>
    </bean>

<!-- Custom channel for console output using the router -->

    <!-- Console Channel 
    <int:channel id="consoleAdapterChannel" />
    <int:outbound-channel-adapter channel="consoleAdapterChannel" ref="consoleAdapter" method="printToStdout" />
    <bean id="consoleAdapter" class="com.idna.dm.logging.activity.util.StdoutTargetAdapter" />
     -->

    <!-- Log4j Channel -->
    <int:channel id="loggingChannel" />
    <int:logging-channel-adapter auto-startup="true" level="INFO" log-full-message="true" channel="loggingChannel" />

<!-- Router -->
    <int:gateway id="activityLoggingGateway" 
        service-interface="com.idna.dm.logging.activity.logger.ActivityLoggingGateway" />


    <int:channel id="activityLoggingChannel" />

    <int:router input-channel="activityLoggingChannel" ref="dynamicRouter"
        method="route" default-output-channel="asyncInProcessChannel"
        ignore-channel-name-resolution-failures="true" />

    <bean id="dynamicRouter" class="com.idna.dm.logging.activity.router.DynamicRouter">
        <constructor-arg index="0" value="asyncInProcessChannel" />
    </bean>

【问题讨论】:

  • 好主意,请问有什么问题?
  • 对不起,我的问题不再相关,但我想找到一种方法来恢复到异步通道作为 ActiveMQ 的故障转移。因此,如果 MQ 出现故障,那么我将完全使用 MQ。我们现在有一个单独的活动 MQ 实例作为故障转移服务器,因此这不再是问题。

标签: java activemq spring-integration


【解决方案1】:

你可以使用failover机制来实现它:

<channel id="input">
  <dispatcher load-balancer="none"/>
</channel>

<service-activator input-channel="input" order="1"/>

<service-activator input-channel="input" order="2"/>

在这种情况下,第一个 &lt;service-activator&gt; 将始终首先被调用。 第二个只有在第一个失败的情况下。

failover 默认为true。

【讨论】:

  • 仅晚了 3 年 ;) 这些天继续使用 Spring 与 RabbitMQ 的集成。
猜你喜欢
  • 1970-01-01
  • 2014-08-23
  • 2021-05-27
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 2017-05-24
  • 2019-04-02
相关资源
最近更新 更多