Showing posts with label Mule. Show all posts
Showing posts with label Mule. Show all posts

Tuesday, November 3, 2015

Mule cache scope caches session variables

Use of caching scope can result in a message's session variables being replaced with the values in a cached message.

Here a (Mule 3.5.2) sub flow contains a component that calls an injected endpoint:

<sub-flow name="transform_event_data_to_list_of_pojo">
    ...
    <component doc:name="Event JSON to List of POJO">
      <prototype-object class="com.xyz.EventJsonToPojoTransformer">
      </prototype-object>
      <binding interface="com.xyz.DbBindingInterface" method="lookupEventAndCategory">
        <outbound-endpoint ref="eventAndCategoryLookupEndpoint" exchange-pattern="request-response" />
      </binding>
    </component>
    ...
</sub-flow>

The endpoint consumer is a flow that caches database queries:

<flow name="eventLookupFlow">
...
<ee:cache doc:name="Cache lookup result" cachingStrategy-ref="Caching_Strategy_Event_And_Category">
  <db:select config-ref="dbConfig" maxRows="1" transactionalAction="NOT_SUPPORTED">
    <db:parameterized-query><![CDATA[SELECT NAME FROM EVENT_AND_CATEGORY_MAPPING WHERE EVENT_TYPE = #[payload[0]]]]></db:parameterized-query>
  </db:select>
</ee:cache>
...
</flow>

The consequence of this is that when control returns to the transform_event_data_to_list_of_pojo flow, the session variables are changed to the session variables in the cached message.

The solution is to target the result of the cache lookup to a flow variable:

<enricher target="#[flowVars['resultSet']]">
  <ee:cache doc:name="Cache lookup result" cachingStrategy-ref="Caching_Strategy_Event_And_Category">
    <db:select config-ref="dbConfig" maxRows="1" transactionalAction="NOT_SUPPORTED">
      <db:parameterized-query><![CDATA[SELECT NAME FROM EVENT_AND_CATEGORY_MAPPING WHERE EVENT_TYPE = #[payload[0]]]]></db:parameterized-query>
    </db:select>
  </ee:cache>
</enricher>







Monday, September 21, 2015

How to use a MuleClient to send a Map message to a JMS queue

An example of using a MuleClient to send a Map message to a JMS queue.  The important step is to put a JmsConnector in the Mule registry.


MuleClient client = new MuleClient(true);

MuleContext mc = client.getMuleContext();

MuleRegistry reg = mc.getRegistry();

org.apache.activemq.ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL("tcp://localhost:61616");

JmsConnector connector = new JmsConnector(mc);
connector.setConnectionFactory(connectionFactory);

reg.registerConnector(connector);

HashMap<String, Object> map = ...

client.dispatch(JMS_QUEUE_INBOUND, map, null);

Thursday, September 3, 2015

Mule Message Enricher ClassCastException Workaround

Mule's message enricher is buggy.  In my flow I have the following:

<enricher doc:name="map channel id">
       <flow-ref name="ChannelEnrichmentFlow" doc:name="channel enrichment flow" />
       <enrich target="#[payload.channelId]" source="#[payload[0].get('CHANNELID')]"></enrich>
       <enrich target="#[payload.uom]" source="#[payload[0].get('UOM')]"></enrich>
       <enrich target="#[payload.uomConversionFactor]" source="#[payload[0].get('UOMCONVERSIONFACTOR')]"></enrich>
</enricher>

Intermittently Mule will report a ClassCastException:

Message               : Execution of the expression "payload.channelId=__object_for_enrichment" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: StormSnapshotData
Code                  : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. java.lang.ClassCastException@13838f4b (java.lang.IllegalArgumentException)
  sun.reflect.GeneratedMethodAccessor99:-1 (null)
2. unable to bind property (java.lang.RuntimeException)
  org.mule.mvel2.optimizers.impl.refl.nodes.SetterAccessor:43 (null)
3. Execution of the expression "payload.channelId=__object_for_enrichment" failed. (org.mule.api.expression.ExpressionRuntimeException)

The following code is equivalent to the enricher and does not cause exceptions:

<enricher target="#[flowVars['resultSet']]" source="#[payload[0]]" doc:name="map channel id">
       <flow-ref name="ChannelEnrichmentFlow" doc:name="channel enrichment flow" />
</enricher>

<expression-transformer expression="#[payload.setChannelId(flowVars['resultSet']['CHANNELID']);payload.setUom(flowVars['resultSet']['UOM']);payload.setUomConversionFactor(flowVars['resultSet']['UOMCONVERSIONFACTOR']);message.payload]" doc:name="enrich payload"/>

This targets the enricher to a local variable (a map), then uses an expression transformer to call setters on the payload object, passing values from the map as parameters. The payload is a Java bean.