Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

How to consume REST web service with ActiveMQ in Mule

I have a requirement where I need to expose a REST web service in Mule .. I have the following flow in Mule :-

   <flow name="MainService" doc:name="MainService">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"/>
        <logger message="RequestLog :- #[message.payloadAs(java.lang.String)]" level="INFO" doc:name="RequestLogger"/>
         <jms:outbound-endpoint queue="NewQueue" connector-ref="Active_MQ" doc:name="JMS" exchange-pattern="request-response"/>
    </flow>


    <flow name="testFlow1" doc:name="testFlow1" initialState="started">
       <jms:inbound-endpoint connector-ref="Active_MQ" address="jms://tcp:NewQueue" doc:name="JMS" exchange-pattern="request-response" disableTemporaryReplyToDestinations="true" responseTimeout="90000"/>
        <jersey:resources doc:name="REST">
            <component class="com.test.services.schema.maindata.v1.Impl.MainDataImpl"/>
        </jersey:resources>
    </flow>

Now I want whenever the service is triggered from a rest client the request should first enter into an ActiveMQ queue NewQueue in MainService flow and in testFlow1 flow it will get the request from NewQueue and will execute it ..

Now there are 2 methods in this webservice :-

Now when ever I trigger the value I get the following exception :-

INFO  2014-08-10 20:23:56,622 [[test].connector.http.mule.default.receiver.02] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'Active_MQ.dispatcher.2028594834'. Object is: EeJmsMessageDispatcher
INFO  2014-08-10 20:23:56,622 [[test].connector.http.mule.default.receiver.02] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'Active_MQ.dispatcher.2028594834'. Object is: EeJmsMessageDispatcher
ERROR 2014-08-10 20:23:56,709 [ActiveMQ Session Task-1] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Failed to invoke JerseyResourcesComponent{testFlow1.component.1820680768}. Component that caused exception is: JerseyResourcesComponent{testFlow1.component.1820680768}. Message payload is of type: String
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. null (java.lang.NullPointerException)
  org.mule.module.jersey.JerseyResourcesComponent:116 (null)
2. Failed to invoke JerseyResourcesComponent{testFlow1.component.1820680768}. Component that caused exception is: JerseyResourcesComponent{testFlow1.component.1820680768}. Message payload is of type: String (org.mule.component.ComponentException)
  org.mule.component.AbstractComponent:144 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/component/ComponentException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.lang.NullPointerException
    at org.mule.module.jersey.JerseyResourcesComponent.doInvoke(JerseyResourcesComponent.java:116)
    at org.mule.component.AbstractComponent.invokeInternal(AbstractComponent.java:122)
    at org.mule.component.AbstractComponent.access$000(AbstractComponent.java:57)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

ERROR 2014-08-10 20:23:57,746 [ActiveMQ Session Task-2] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : "Message with id "ID:ANIRBAN-PC-50216-1407682432183-1:1:6:1:1" has been redelivered 1 times on endpoint "jms://tcp:NewQueue", which exceeds the maxRedelivery setting of 0 on the connector "Active_MQ". Message payload is of type: ActiveMQTextMessage
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. "Message with id "ID:ANIRBAN-PC-50216-1407682432183-1:1:6:1:1" has been redelivered 1 times on endpoint "jms://tcp:NewQueue", which exceeds the maxRedelivery setting of 0 on the connector "Active_MQ". Message payload is of type: ActiveMQTextMessage (org.mule.transport.jms.redelivery.MessageRedeliveredException)
  org.mule.transport.jms.redelivery.JmsXRedeliveryHandler:81 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/transport/jms/redelivery/MessageRedeliveredException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.transport.jms.redelivery.MessageRedeliveredException: "Message with id "ID:ANIRBAN-PC-50216-1407682432183-1:1:6:1:1" has been redelivered 1 times on endpoint "jms://tcp:NewQueue", which exceeds the maxRedelivery setting of 0 on the connector "Active_MQ". Message payload is of type: ActiveMQTextMessage
    at org.mule.transport.jms.redelivery.JmsXRedeliveryHandler.handleRedelivery(JmsXRedeliveryHandler.java:81)
    at org.mule.transport.jms.MultiConsumerJmsMessageReceiver$JmsWorker.preProcessMessage(MultiConsumerJmsMessageReceiver.java:512)
    at org.mule.transport.AbstractReceiverWorker$1$1.process(AbstractReceiverWorker.java:117)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

Now I guess the request need to be inserted into the ActiveMQ queue NewQueue should be String format in case of insertDataOperation as the request is on BODY .. and I am not sure for retrieveDataOperation which carries value id in url ...

Please let me know how to fix the exception for both the cases ... Please help

Upvotes: 0

Views: 1129

Answers (2)

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

So as per David's suggestion need to add :-

<mulexml:object-to-xml-transformer acceptMuleMessage="true" doc:name="Object to XML"/>

UPDATED WORKING FLOW :-

<jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>

<flow name="MainService" doc:name="MainService">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"/>
<logger message="RequestLog :- #[message.payloadAs(java.lang.String)]" level="INFO" doc:name="RequestLogger"/>
<mulexml:object-to-xml-transformer acceptMuleMessage="true" doc:name="Object to XML"/>
<jms:outbound-endpoint queue="NewQueue" connector-ref="Active_MQ" doc:name="JMS" exchange-pattern="request-response"/>
</flow>

<flow name="testFlow1" doc:name="testFlow1" initialState="started">
 <jms:inbound-endpoint connector-ref="Active_MQ" address="jms://tcp:NewQueue" doc:name="JMS" exchange-pattern="request-response" disableTemporaryReplyToDestinations="true" responseTimeout="90000"/>
 <mulexml:xml-to-object-transformer doc:name="XML to Object"/>
 <jersey:resources doc:name="REST">
   <component class="com.test.services.schema.maindata.v1.Impl.MainDataImpl"/>
 </jersey:resources>
</flow>

Upvotes: 0

David Dossot
David Dossot

Reputation: 33413

The most prominent problem is that you only send the payload of the HTTP request via the JMS endpoint, which is of course not enough for the Jersey to be happy.

You need to serialize the whole Mule message to XML (so it's properties are carried over too) ...

<xml:object-to-xml-transformer acceptMuleMessage="true" />

... then send it over JMS, then deserialize it before the Jersey component.

TBF I don't know if this will work but that's probably the closest to a working solution you will be.

Upvotes: 1

Related Questions