I need to set the global property grammatically. Functionally it should do exactly what the following statement should be doing
<global-property name="host-name" value="localhost" doc:name="Global Property"/>
I believe I should be able to access this property using ${host-name}. In this specific scenarion I don't want to load the property from properties file.
If you just want to resolve placeholders like ${host-name} you can extend the Spring PropertyPlaceholderConfigurer class with a custom implementation of method resolvePlaceholder, and add it as a Spring bean like this:
<spring:beans>
<spring:bean id="myConfigurer" class="my.test.MyConfigurer"/>
</spring:beans>
Your custom resolvePlaceholder is then called to resolve any unresolved properties.
It is also fairly simple to set actually set properties in the Mule context registry by getting the context from a message (message.getMuleContext()) or by implementing a MuleContextAware bean, but in those cases the property will not yet be available at the time when the property placeholders are resolved.
You would set the property like this:
context.getRegistry().registerObject("myKey", "myVal")
and access it later like this:
context.getRegistry().get("myKey")
Related
I would like to know what would be the best way to load the properties file in mule for the following scenario.
I have a properties file where in I set all the email related fields set in it like templates, to and from etc.
All these need to be set to a specific object along with other changes to that object so I'm planning to use a Java transformer and now I need to load all those values from that properties file and send to transformer. So what would be a best approach to work in above scenario.
Load properties in Java transformer using core java load properties
Load properties using spring context and send it to transformer and access using inbound properties
Kindly let me know if there is any other better approach other than above
First, you should be able to load the property file using spring context as shown below:
<context:property-placeholder location="somename.properties" />
Then, you can set specific property value as flow variable as shown below:
<set-variable variableName="fromAddress" value="${xyz.fromAddress}" />
Finally, you can access this flow variable in your processor class as shown below:
String fromAddress = muleEvent.getFlowVariable("fromAddress");
I will suggest to use Java load properties if these properties are only used in one Transformer. There is one more benefit out of Java load properties is that after modifying your properties file you do not need to restart the application.
I have a line that reads like this in my flow:
<jms:inbound-endpoint queue="${queue.name}" connector-ref="${inbound.connector}" doc:name="Inbound Endpoint">
Where the "${inbound.connector}" property refers to a string in my properties file:
inbound.connector=Active_MQ
The reason I am doing this is because the connector-ref will vary depending on the environment. Sometimes it will be an Active_MQ connector, sometimes it will be a JMS Connector.
The properties file is under src/main/resources, as it should be. I have other properties in the flow that are read from the properties file just fine, such as the "queue.name" property. However, for some reason when I try to start Mule it is returning an error saying:
NoSuchBeanDefinitionException: No bean named '${inbound.connector}' is defined.
As far as I know the connector-ref value is just a string, so this should work in theory. I don't understand this error. Is the connector-ref in fact not a string, and this approach is illegal?
Internally mule runtime creates/initializes all the connector objects(spring bean) like HTTP Connectors, DATASOURCE connectors and various other connector's when the application comes up. All these connectors are nothing but a spring beans as Mule is written on top of Springframework. So it is not possible to make it dynamic by passing in a string value where it expects a object.
I am trying to access the _clienName from the invocation properties. I tried the below one, but doesnt help. any thoughts ? Is it really possible to access invocation property in a mule flow ?
#[message.invocationProperties['_clientName']]
Message properties:
INVOCATION scoped properties:
__httpEvent=HttpEvent #188...9{apiName=.....95574017,something=99034}
_clientId=680.....81d97344
_clientName=abcd-app
INBOUND scoped properties:
Invocation Properties can be accessed via flowVars: #[flowVars['_clientName']] or #[flowVars._clientName]
Post Mule EE 3.4 there has been a naming change on all the invocation property as FlowVariables . Even now the invocation properties will work if you try using #[flowVars.name] . In the Message properties transformer you have option to set only as invocation properties but it can be accessed as flowVars in rest of the flow.
In mule invocation scoped properties are nothing but flowVars. So you can access them by using the following mule expression
#[flowVars.'_clientName']
in the above expression no need to wrap the _clientName in square brackets, just quote them as I did.
FYI
Session scoped properties are nothing but sessionVars. So you can access them by using #[sessionVars.'_clientName'].
Though you have not asked about session scoped properties, I have answered it because by reading your question I came to know that you are asking the question by seeing the logs in the console caused by the Logger Component of Mule.
The logger, not only logs Invocation scoped properties but also session scoped properties as well. So I guess you might get a question in your mind about what are Session scoped properties, so answered it.
I have a properties file shared on several apps. To access this properties into one app, I use this tag:
<context:property-placeholder location="classpath:br/com/empresa/configuracao/mule-apps.properties"/>
On several Mule components, like database attributes connections, I use the following expression to access the properties, p.e.: ${db.user}. It works!
But on Java Transformer, how I access the properties?
I tried the following instructions, but returned null:
System.getProperty("db.user");
message.getInboundProperty("db.user");
message.getOutboundProperty("db.user");
message.getInvocationProperty("db.user");
Is there a way to access properties into Mule Java Transformer?
This question has been answered for components here: How to get property from context property placeholder tag inside custom java component The exact same logic applies to transformers.
Use property injection:
<custom-transformer class="org.myCompany.CustomTransformer">
<property name="dbUser" value="${db.user}" />
</custom-transformer>
Don't forget to add setDbUser on your custom component!
You could use the old way of retrieving a property:
#Value("${db.user")
private String dbUser;
I'm on Mule ESB 3.6 EE.
Trying to figure out how to leverage DataMapper component for populating properties of a global bean.
Basically, I have a bean defined like this:
<spring:beans>
<spring:bean id="testBean" name="testBean" class="com.test.MyTest" scope="prototype"/>
</spring:beans>
and I would like to populate it with using DataMapper. However, I can't figure out how to specify the bean object name (testBean) in the mapper wizard. The only option I can see is POJO where I can specify the class name but not the particular bean name.
So, any ideas how this could be solved?