Producer的ConnectionFactories&消费者

时间:2014-01-23 05:22:49

标签: apache-camel

我正在使用Camel。请告诉我以下方法是否合适。

制片人CCF

<?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:camel="http://camel.apache.org/schema/spring"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="hornetConnectionFactory" class="org.hornetq.jms.client.HornetQJMSConnectionFactory">
        <constructor-arg name="ha" value="false"></constructor-arg>
        <constructor-arg>
            <bean id="transportConfiguration" class="org.hornetq.api.core.TransportConfiguration">
                <constructor-arg
                    value="org.hornetq.core.remoting.impl.netty.NettyConnectorFactory" />
                <constructor-arg>
                    <map key-type="java.lang.String" value-type="java.lang.Object">
                        <entry key="host" value="127.0.0.1" />
                        <entry key="port" value="5445" />
                    </map>
                </constructor-arg>
            </bean>
        </constructor-arg>
    </bean>

    <!-- ConnectionFactory Definition -->
    <bean id="connectionFactory"
        class="org.springframework.jms.connection.CachingConnectionFactory">
        <constructor-arg ref="hornetConnectionFactory" />
    </bean>

    <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory" ref="connectionFactory" />
    </bean>

    <context:component-scan base-package="com.camlin.producer" />

    <camel:camelContext id="camel-server">
        <camel:package>com.camlin.producer</camel:package>
    </camel:camelContext>
</beans>

消费者的正常CF,

<?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:camel="http://camel.apache.org/schema/spring"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="hornetConnectionFactory" class="org.hornetq.jms.client.HornetQJMSConnectionFactory">
        <constructor-arg name="ha" value="false"></constructor-arg>
        <constructor-arg>
            <bean id="transportConfiguration" class="org.hornetq.api.core.TransportConfiguration">
                <constructor-arg
                    value="org.hornetq.core.remoting.impl.netty.NettyConnectorFactory" />
                <constructor-arg>
                    <map key-type="java.lang.String" value-type="java.lang.Object">
                        <entry key="host" value="127.0.0.1" />
                        <entry key="port" value="5445" />
                    </map>
                </constructor-arg>
            </bean>
        </constructor-arg>
    </bean>

    <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory" ref="hornetConnectionFactory" />
    </bean>

    <context:component-scan base-package="com.camlin.consumer" />

    <camel:camelContext id="camel-consumer">
        <camel:package>com.camlin.consumer</camel:package>

        <camel:route id="one">
            <camel:from uri="jms:queue:request?selector=type='1'" />
            <camel:to uri="bean:consumerBean?method=receive1" />
        </camel:route>

        <camel:route id="two">
            <camel:from uri="jms:queue:request?selector=type='2'" />
            <camel:to uri="bean:consumerBean?method=receive2" />
        </camel:route>
    </camel:camelContext>
</beans>

1 个答案:

答案 0 :(得分:0)

对于Producers,您应该使用JMS连接池...通常是AMQ的PooledConnectionFactory或Spring的CachingConnectionFactory

对于消费者,你应该尽可能使用Spring的DefaultMessageListenerContainer而不是JmsTemplate.receive()

请参阅JmsTemplate最佳做法:http://activemq.apache.org/jmstemplate-gotchas.html

和本文了解更多详情:http://codedependents.com/2009/10/16/efficient-lightweight-jms-with-spring-and-activemq/