2016-01-27 67 views
1

我是很新,JMS和有一個要求AMQ和WMQ之間的橋樑,我的駱駝文檔中看到,而不是使用JMS到JMS橋他們推薦使用駱駝。駱駝的JMS到JMS橋 - 不表示作爲一個消費者AMQ

首先,我試圖讓我的應用程序將消息從AMQ中取出,並簡單地記錄它是這樣做的,但是每當我在Jetty中啓動我的應用程序時,它都不會在apiToTopsQueue上顯示爲消費者,因此不會將消息從隊列中取出。

我的應用程序的context.xml(它加載駱駝上下文)

<?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" 
     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"> 

    <context:property-placeholder location="classpath:tops-bridge.properties" /> 

    <import resource="classpath:camel-context.xml" /> 

    <bean id="log4jInitialization" 
      class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
     <property name="targetClass" value="org.springframework.util.Log4jConfigurer" /> 
     <property name="targetMethod" value="initLogging" /> 
     <property name="arguments"> 
      <list> 
       <value>classpath:log4j.xml</value> 
       <value>60000</value> <!-- Refresh Log4j config every 60 seconds --> 
      </list> 
     </property> 
    </bean> 
</beans> 

那麼這是駱駝的context.xml(我一直在進出隨機碼從教程評論,所以可能很奇怪)

<?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:broker="http://activemq.apache.org/schema/core" 
     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://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> 

    <camel:camelContext id="defaultCamelContext"> 
     <camel:routeBuilder ref="bridgeRouteConfig"/> 
     <!--<camel:jmxAgent id="agent" createConnector="true"/>--> 
    </camel:camelContext> 

    <bean id="bridgeRouteConfig" class="com.caci.asg.rail.tops.bridge.TopsBridgeRouteBuilder"> 
     <constructor-arg name="amqToBridgeQueue" value="${topsBridgeRouteBuilder.route.amqToBridgeRoute}"/> 
     <constructor-arg name="bridgeToWmqQueue" value="${topsBridgeRouteBuilder.route.bridgeToWmqRoute}"/> 
     <constructor-arg name="wmqToBridgeQueue" value="${topsBridgeRouteBuilder.route.wmqToBridgeRoute}"/> 
     <constructor-arg name="bridgeToAmqQueue" value="${topsBridgeRouteBuilder.route.bridgeToAmqRoute}"/> 
    </bean> 

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

    <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" 
      init-method="start" destroy-method="stop"> 
     <property name="maxConnections" value="8"/> 
     <property name="connectionFactory" ref="jmsFactory"/> 
    </bean> 

    <bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"> 
     <property name="connectionFactory" ref="pooledConnectionFactory"/> 
     <property name="concurrentConsumers" value="10"/> 
    </bean> 


    <!-- lets configure the ActiveMQ JMS broker server --> 
    <broker:broker useJmx="true" persistent="false" brokerName="myBroker"> 
     <broker:transportConnectors> 
      <!-- expose a VM transport for in-JVM transport between AMQ and Camel on the server side --> 
      <broker:transportConnector name="vm" uri="vm://myBroker"/> 
      <!-- expose a TCP transport for clients to use --> 
      <broker:transportConnector name="tcp" uri="${application.activemq.url}"/> 
     </broker:transportConnectors> 
    </broker:broker> 

    <!-- lets configure the Camel ActiveMQ to use the embedded ActiveMQ broker declared above --> 
    <bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent"> 
     <property name="brokerURL" value="vm://myBroker"/> 
    </bean> 

</beans> 

駱駝-context.xml中利用一些性能,這是如下的(那些指WMQ是在此刻,直到我AMQ工作)的佔位符。

application.activemq.url=tcp://localhost:61616 

topsBridgeRouteBuilder.route.amqToBridgeRoute=jms:apiToTopsQueue 
topsBridgeRouteBuilder.route.bridgeToWmqRoute=mq:toWmq 
topsBridgeRouteBuilder.route.wmqToBridgeRoute=mq:fromWmq 
topsBridgeRouteBuilder.route.bridgeToAmqRoute=jms:topsToApiQueue 

路線建設者是在Java中如下

import org.apache.camel.LoggingLevel; 
import org.apache.camel.builder.RouteBuilder; 

public class TopsBridgeRouteBuilder extends RouteBuilder { 

    private final String amqToBridgeQueue; 
    private final String bridgeToWmqQueue; 
    private final String wmqToBridgeQueue; 
    private final String bridgeToAmqQueue; 

    public TopsBridgeRouteBuilder(String amqToBridgeQueue, String bridgeToWmqQueue, String wmqToBridgeQueue, String bridgeToAmqQueue) { 
     this.amqToBridgeQueue = amqToBridgeQueue; 
     this.bridgeToWmqQueue = bridgeToWmqQueue; 
     this.wmqToBridgeQueue = wmqToBridgeQueue; 
     this.bridgeToAmqQueue = bridgeToAmqQueue; 
    } 

    @Override 
    public void configure() throws Exception { 
//  from(amqToBridgeQueue).to(bridgeToWmqQueue).log(LoggingLevel.INFO, "Message moving to " + bridgeToWmqQueue); 
//  from(wmqToBridgeQueue).to(bridgeToAmqQueue).log(LoggingLevel.INFO, "Message moving to " + bridgeToAmqQueue); 

     from(amqToBridgeQueue).log(LoggingLevel.WARN, "Consuming message from" + amqToBridgeQueue); 
    } 
} 

所以,我真的不知道爲什麼它沒有被列爲apiToTopsQueue的消費者,當我開始碼頭。我的pom包含AMQ/WMQ/camel庫的依賴關係。依賴如下(繼承自父POM版本/範圍)

<dependency> 
      <groupId>org.apache.camel</groupId> 
      <artifactId>camel-core</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.camel</groupId> 
      <artifactId>camel-spring</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.camel</groupId> 
      <artifactId>camel-jms</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.ibm.mq</groupId> 
      <artifactId>com.ibm.mq.jmqi</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.ibm.mq</groupId> 
      <artifactId>com.ibm.mqjms</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.ibm.mq</groupId> 
      <artifactId>connector</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.ibm.mq</groupId> 
      <artifactId>dhbcore</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.activemq</groupId> 
      <artifactId>activemq-all</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.activemq</groupId> 
      <artifactId>activemq-pool</artifactId> 
     </dependency> 

我寫了這個測試,看看我可以把信息上,並把他們從我橋AMQ - 這工作,在AMQ管理頁面我可以看到入隊/出隊消息計數遞增。

import org.apache.activemq.ActiveMQConnectionFactory; 
import org.junit.Test; 

import static org.hamcrest.core.Is.is; 
import static org.junit.Assert.assertThat; 

import javax.jms.*; 

public class TopsBridgeRouteBuilderTest { 

    @Test 
    public void testAMessageAddedToAmqCanBeRetrieved() throws JMSException { 

     String brokerURL = "tcp://localhost:61616"; 
     String amqQueue = "apiToTopsQueue"; 
     String messageToSend = "Test message"; 

     // Put a message on the AMQ 
     ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURL); 
     Connection connection = connectionFactory.createConnection(); 
     connection.start(); 
     Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
     Destination destination = session.createQueue(amqQueue); 
     MessageProducer producer = session.createProducer(destination); 
     producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 
     TextMessage tm = session.createTextMessage(messageToSend); 
     producer.send(tm); 

     // Create read only consumer to take the message off the queue 
     ActiveMQConnectionFactory connectionFactoryReadOnly = new ActiveMQConnectionFactory(brokerURL); 
     Connection connectionReadOnly = connectionFactoryReadOnly.createConnection(); 
     connectionReadOnly.start(); 
     Session sessionReadOnly = connectionReadOnly.createSession(false, Session.AUTO_ACKNOWLEDGE); 
     MessageConsumer consumer = sessionReadOnly.createConsumer(destination); 
     final TextMessage message = (TextMessage) consumer.receive(); 

     System.out.println("Message retrieved = " + message.getText()); 
     assertThat(message.getText(), is(messageToSend)); 
    } 
} 

在我的配置中是否有什麼錯誤,意味着當我啓動Jetty時,Camel路由不工作或正在查看AMQ?

謝謝。

回答

0

原來我缺少我的web.xml本節。爲未來學到的教訓!

<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener>