Java Component in Mule does not use project's UTF-8 encoding - mule

I have a java component in Mule that is returning a String. When I print that string from within the java component, it reads as UTF-8 just fine in the console. However, when I return this as the payload, the subsequent logger and all downstream components can no longer read the UTF-8 characters. I've confirmed that my project's encoding setting is set to 'UTF-8'. I've also:
Setting MULE_ENCODING=UTF-8 before and after the Java component, both as an outbound property and invocation property.
Setting the encoding on the message in a mule expression, but that failed message.setEncoding('UTF-8') with an error stating it could not find the method setEncoding()
Using the following in the java component:
eventContext.getMessage().setPayload(myString);
eventContext.getMessage().setEncoding("UTF-8");
return eventContext.getMessage().getPayload();
Switching to a java transformer instead of a component, using the below config on the transformer, explicitly setting encoding to "UTF-8".
<custom-transformer encoding="UTF-8"
class="myDomain.myJavaTransformer" doc:name="Java"/>
I'm not sure what else to try to ensure the payload coming out of the java component/transformer is not interpreted or converted to another encoding...
The output should read: “Outstanding New Environmental Scientist”
The logger currently outputs is “Outstanding New Environmental Scientistâ€. I've corrected this in other parts of the flow by adding encoding=UTF-8 to any VM endpoints, for example. I've also confirmed that this can print correctly in a logger elsewhere in the flow.
There seems to be something happening after the String is returned from within the java class referenced by the java component and the subsequent mule logger...

try <object-to-string-transformer encoding="..." /> right after your java component

I had a problem with encoding too. It was not completely similar to your case but I took this action and it worked for me. (Byte to array using java serialization)
<set-property propertyName="MULE_ENCODING" value="UTF-8" doc:name="Property" />
<byte-array-to-string-transformer encoding="UTF-8" doc:name="Byte Array to String"/>
I used that when message is coming to my flow.
Also there is properties in wrapper and you can set that as well.

How are you reading the RSS feed? If it is using an endpoint, try setting the 'encoding' attribute for that endpoint?

Related

Calling a flow using Groovy Script in Mule 4

I need to make a call to a flow using groovy script in mule 4. Can someone let me know if they have tried it using Mule 4
It is possible, but I would HIGHLY recommend that you do NOT do it.
You will have to dig deep down into the Mule Java APIs and couple yourself to them and also add a lot of your own error handling etc. and be wary of platform updates and the if the Java API changes.
Personally I would restructure you app to only have 'business logic' in scripts and let Mule do the flow lookups elsewhere.
You can even lookup flows in dataweave.
But here is a rough working example(Note you will have to harden this code yourself):
<flow name="test-flow">
<scheduler>
<scheduling-strategy>
<fixed-frequency frequency="100000"></fixed-frequency>
</scheduling-strategy>
</scheduler>
<set-payload value="bla bla" />
<scripting:execute engine="groovy" doc:name="Toggle flow" doc:id="2eb6f071-bdef-4d3d-926d-2565fcd62d33">
<scripting:code>
import org.mule.runtime.api.message.Message;
import org.mule.runtime.core.api.event.CoreEvent;
import org.mule.runtime.core.api.event.EventContextFactory;
flow=registry.lookupByName("another-flow").get();
thisflow=registry.lookupByName("test-flow").get();
msg = Message.builder().value(payload).build();
event =CoreEvent.builder(EventContextFactory.create(thisflow,
org.mule.runtime.dsl.api.component.config.DefaultComponentLocation.fromSingleComponent("add-location"))).message(msg).build();
result =flow.process(event);
</scripting:code>
</scripting:execute>
</flow>
<flow name="another-flow">
<logger level="ERROR" message="Another Flow #[payload]" />
</flow>
You need to fetch the current flow and the flow you want to call.
Using the Mule APIs - construct a Mule Event, Messgae, payload etc. You will also need to add any attributes you need etc.
Is there any way to call mule "static flow" like When I use the above groovy script and it will call the flow ( its starting the called flow and get the values) and get the values.
Requirement :
Like, Mule Flow A is running independently.
Mule flow B wants to access the Flow A variable with out starting the flow A using groovy or python Script.
Note: Can do with flow reference and lookup but would like to implement using script.

Message Enricher in Mule

Trying to understand when to use Message Enricher in Mule? Can someone explains with real usage would be great. I already gone through mule documentation examples
Mule message enricher is best used in the case when you do not to lose your existing payload.
For example, let's consider, you need to call a DB in the middle of the flow and by doing this you will loose your current payload with the data from the DB and you don't want this to happen.
So, here you need your DB component wrapped with message enricher. By doing that you will get the data from DB as well as you will not lose your existing current payload.
<enricher target="#[flowVars.recordFound]" doc:name="Message Enricher">
<db:select config-ref="Derby_Configuration1" doc:name="DB_Details">
<db:parameterized-query><![CDATA[select * from Table1]]></db:parameterized-query>
</db:select>
</enricher>
Since message enricher takes a copy of the current payload, your existing payload will not lost
For more idea you can refer here:- http://www.slideshare.net/anir37/mule-message-enricher
Message Enricher will be used to store the data and to check that data in future processing like if you have to do batch processing so that it has to check the data whether present or not in first batch, if not we can insert that data in second batch.
It is like enrich the message which does not have in source system.
One common scenario involves the need to enrich an incoming message with information that isn’t provided by the source system. You can use a content enricher if the target system needs more information than the source system can provide.
Consider a message from a source system contains a zip code but the target system needs the two letter state. A message enricher can be used to lookup the state using the zip code from an enrichment resource. The enricher calls out to the enrichment resource with the current message (containing the zip code) then enriches the current message with the result.
This is a very simple flow with one-way inbound and outbound endpoints, and which acts as part of an order processing pipeline. This flow uses an enricher to add a state flow variable to the current message with the state that the stateLookup endpoint returns. The ‘target’ attribute defines how the current message is enriched using a MessageEnricher which uses the same syntax as expression evaluators.
Mule Enricher is a component, whole idea of using an enricher is to maintain the payload transaction intact and use it's content to apply some logic and store the result in a variable.
e.g. can be a xml payload, used to derive a complex logic result like discount calculation and then result of it can be stored in a variable and can be used further.
To add to these answers, you may also set the returning message (e.g. payload) to either payload, flow variables and outbound properties by simply defining the source and target within the Message Enricher.
The main purpose of the Message Enricher in Mule is to carry the same payload even after calling another flow by using flow reference.
Use Case: If you wanna use the same payload data through out the flow then you should go for Message Enricher
As you can see in the attached screenshot, here I have two flow references one is under Message Enricher i.e the Message Enricher flow reference will prevent the payload to be overridden by the payload from the referenced flow(overridenFlow), the original payload is maintained so that it can be used further
enter image description here
After second flow reference the payload is overridden and original payload can't be accessed further.
Message enricher is mainly used to extract something(eg. xpath) from payload and store it into flow/session variable, without actually changing the payload.
More advantages:
1) The source can be anything(i.e. payload, variables etc)
2) The source can also be modified to be able to extract info from it.
3) Multiple sources and target option is also available.
Also, as only one component can reside inside Message Enricher. Use of processor chain is recommended if more components are needed to reside inside Message Enricher.
Message Enricher in Mule is used to carry the same payload even after calling another flow by using flow reference.
Example use case: If you are taking data from some SaaS application and then want to store that data in Database, such that there is no duplicate entry in the Database. In this case you can check for Duplicate Data with the help of message Enricher before inserting it into Database.
Message enricher is one of the important components with Mulesoft. There are various scenarios in which you can use the message enricher component.
When synchronizing data between data sources, you often check to see if a record already exists in the target resource.
If you simply add an endpoint to query the target resource first before adding it, the response will become the payload. This is not what you want!
You want the external call to act as an enrichment of the existing message with the original payload retained.
Message Enricher in Mule is used to carry the same payload even after calling another flow by using flow reference.

Mule ESB: Multiple instances of custom transformer are created during server start up

I created a new custom transformer:
MyCustomTransformer, that extends AbstractMessageTransformer.
Than I defined:
<custom-transformer name ="myCustomTransformer"class="com.acme.MyCustomTransformer"/> in Mule xml file.
And on the last step I started to use it via:
<transformer ref="myCustomTransformer">
All works as expected, with only one note: I see that 5 instances of MyCustomTransformer created during server startup.
And I have not an idea how to explain it. Why 5 instances? I defined only one.

Is there a way to mock non-global filters in a sub-flow using MUnit?

I wanted to know if there was a way to mock a custom-filter that is within a sub-flow using MUnit.
I'm using Mule 3.4.0 and MUnit 3.4.0.M5.
The sample flow looks like the following.
<sub-flow name="a">
<choice>
<when expression="something...">
<custom-filter doc:name="filter a">...</custom-filter>
</when>
<otherwise>
...
</otherwise>
</choice>
</sub-flow>
I've had to create a wrapper flow around the subflow because I get a NullPointerException whenever I try to hit the sub flow directly using the runFlow syntax. However, in doing so, I am unable to mock the custom-filter using the whenMessageProcessor syntax. Please see my attempt below.
whenMessageProcessor("custom-filter"
.withAttributes(attribute("name").ofNamespace("doc").withValue("filter a"))
.thenReturn(muleMessageWithPayload("some response");
This results in the message not being mocked.
There are to issues here mixed up.
You having to wrap you sub-flow is a MUnit/Mule issue as commented here:
How to mock a Java component within Mule Flow using MUnit
The second issue is filter mocking. Short answer is you can not, please check:
https://github.com/mulesoft/munit/issues/108
Conceptually a filter is a shorthanded way of doing a choice (or an if in a normal language). One usually does not mock a choice/if it rather changes a value in a variable or in our case in the mule message payload. That's why the filter MP can not be mocked.
HTH

How can i setpayload on xml config?

i want to set some message to payload.
i always used filter to set some data to payload.
i think they are some ways to setpayload whith out using filter.
could somebody help me how to setpayload on the mule-config.xml file ?
i tried
<set-payload value="something"/>
but doesn't works .. i am using mule 3.2
thanks!
The set-payload transformer is available starting with Mule 3.3.
For 3.2, the shortest is expression-transformer:
http://www.mulesoft.org/documentation-3.2/display/32X/Expression+Transformer+Reference