Using properties from Jboss configuration in Java.

Honestly, I am a little embarrassed that I did not know this.

However, here is the problem statement.
Use properties defined in the Jboss configuration file ( standalone.xml or domain.xml) in Java.

The properties are defined in the config file as such ( standalone.xml in my case)

<system-properties>
  <property name="APP-NAME" value="MyApp"/>
  <property name="LOG_DIR" value="C:/Logs"/>
</system-properties>

We are all we aware of the fact that properties are defined in an properties file and we can use it in our Java code (in case if you need help with it, please check this post) but to read the properties defined in Jboss config, we will have to do something like this.

String logDir = System.getProperty("LOG_DIR");
String appName = System.getProperty("APP-NAME");

And that’s it. Pretty simple and straight forward.
Now, if for any reason, you have to append properties, do this.
Example: The logs should remain in C:/Logs/MyApp, so apparently, I have append the properties with a “/”. But now the problem is basing on the OS, this separator might differ. In such situations, File.separator comes to rescue as shown below.

String logDir = System.getProperty("LOG_DIR");
String appName = System.getProperty("APP-NAME");
String log = logDir+File.separator+appName

Hope this helps.

Apache Camel – Calling an Endpoint from a Processor

Like most of my posts, this one was a hard find too. Without further ado, let’s dive in.

This was my requirement. Send the data to different endpoints from my source. I could  have simply done this:

from(fromLocation)
  .to(endpoint1).to(endpoint2);

or if I had to send the same data to multiple endpoints

from(fromLocation)
multicast().to("endpoint1","endpoint2");

But my problem is little complicated. I get the data in a List and each of the element in the List has to be delivered to multiple endpoints. So my processor looked like this:

@Override
 public void process(Exchange exchange) throws Exception {

 List
<map> responseList = (List
<map>) exchange.getProperty("responseList");
 
 for(Map response:responseList){
      String responseMsg = (String)response.get("response");
      String responseType = (String)response.get("responseType");
      if(responseType.equals("XYZ")){
           //send message somewhere.
       }
   }
 }</map></map>

Now, how should I send the message somewhere is the real question. After much struggle, I found the solution in ProducerTemplate.

@Autowired
CamelContextManager camelContextManager;

private ProducerTemplate producerTemplate;

@Override
public void process(Exchange exchange) throws Exception {
  List
<map> responseList = (List
<map>)exchange.getProperty("responseList");
  for(Map response:responseList){
	
      String responseMsg = (String)response.get("response");
      String responseType = (String)response.get("responseType");
      if(responseType.equals("XYZ")){
           producerTemplate =camelContextManager.getCamelContext().createProducerTemplate();
	   producerTemplate.send("file:C:\\out", exchange);
       }
	
}</map></map>

And if you are wondering what my CamelContextManager looks like:

@Component
public class CamelContextManager implements CamelContextAware{

	protected CamelContext camelContext;
	
	@Override
	public void setCamelContext(CamelContext camelContext) {
		this.camelContext = camelContext;
	}

	@Override
	public CamelContext getCamelContext() {
		return camelContext;
	}
	

}

So there you go, pretty straight forward. Now if you have to send it another processor for example, you can simply do:

@Autowired
Processor2 processor2;

public void process(Exchange exchange) throws Exception {
......
      if(responseType.equals("XYZ")){
           processor2.process(exchange)
       }
....
}

Obviously I am using Spring container here, but this is the general idea.
Happy Coding!