How to get Java Installation location?

package com.pretech;
public class JavaHome {
	public static void main(String[] args) {
		System.out.println(System.getProperty("java.home"));
	}
}

Output



C:\Program Files\Java\jdk1.7.0_06\jre


How to import/load multiple spring configuration file

In Spring configuration file we can import other context.xml files using <import resource=”xxxx”> tag.

Example

<import resource="classpath:com/pretech/applicationContext.xml"/>

How to convert java enum to string ?

Example

package com.pretech;
public class EnumToStringExample {
	public static void main(String[] args) {
		String days = Days.MONDAY.name();
		System.out.println(days + "    " + days.getClass());
	}
}
enum Days {
	MONDAY
}

Output



MONDAY    class java.lang.String


How to loop Java Enums Example

Example

package com.pretech;
public class EnumLoopExample {
	public static void main(String[] args) {
		for (Week weekdays : Week.values()) {
			System.out.println(weekdays);
		}
	}
}
enum Week {
	MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY,
}

Output



MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY


Camel Selective Consumer using JMS Selector Example

The selective consumer is consumer that applies a filter to incoming messages so that only messages meeting that specific selection criteria are processed. A JMS selector is a predicate expression involving JMS headers and JMS properties. If the selector evaluates to true, the JMS message is allowed to reach the consumer.
Let us consider an Order processing example, we have two types of orders (One is Online and another one is physical store order) All orders are pushing in to ORDER queue along with the details and Two consumers (Online order processor and Physical order processor) are selecting messages based on the predicate expressions.
image
Photo Credit : http://fusesource.com

Prerequisites

ActiveMQ should be up and running

 

1. Create a Maven Project

Create a Maven project with following dependencies
<dependencies>
    <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core</artifactId>
    <version>2.12.1</version>
    </dependency>
    <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring</artifactId>
    <version>2.12.1</version>
    </dependency>
    <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jms</artifactId>
    <version>2.12.1</version>
    </dependency>
    <dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>5.8.0</version>
    </dependency>
     <dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
    <version>5.8.0</version>
    </dependency>
  </dependencies>
 

2. Setup Spring Configuration (camel-application-context-msgselector.xml)

In this configuration we will load the activemq component with name jms and start one route which needs to process the Order Queue.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <bean id="route" class="com.vinod.test.MessageSelectorRoute" />

    <camel:camelContext id="camel-client">
        <camel:template id="camelTemplate" />
        <camel:routeBuilder ref="route" />
    </camel:camelContext>
    <bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
        <property name="brokerURL" value="tcp://localhost:61616" />
    </bean>
</beans>

3. Create a Message Router 

This route has the message selector and it will listen the ORDER Queue and based on the selector it will redirect to the other Queues.
eg: ONLINE order details will go the the online processor and store orders will go the the store processor.
package com.vinod.test;

import org.apache.camel.builder.RouteBuilder;

public class MessageSelectorRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("jms:ORDER?selector=METHOD='ONLINE'").to("jms:queue:ORDERONLINEPROCESSOR");
        from("jms:ORDER?selector=METHOD='STORE'").to("jms:queue:ORDERSTOREPROCESSOR");
    }
}

 

4. Create a Message producer

This class will push the order messages in to ORDER queue and add the selector name as well.
package com.vinod.test;

import org.apache.camel.ProducerTemplate;
import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MessageSelectorMain {

    public static void main(String[] args) throws Exception {

        // Sending Message to the order Queue
        ApplicationContext context = new ClassPathXmlApplicationContext("camel-application-context-msgselector.xml");
        ProducerTemplate camelTemplate = context.getBean("camelTemplate", ProducerTemplate.class);
        System.out.println("Order Message Sending started");
        camelTemplate.sendBodyAndHeader("jms:queue:ORDER", "Online Order Details", "METHOD", "ONLINE");
        camelTemplate.sendBodyAndHeader("jms:queue:ORDER", "Store Order Details", "METHOD", "STORE");
        camelTemplate.sendBodyAndHeader("jms:queue:ORDER", "Online Order Details", "METHOD", "ONLINE");
        System.out.println("Order Message sending completed");

    }

}

Run it

Run the above main program and check the active mq console, we can see the massages are routed in to different queue based on the selector.

Output


Download Example

https://github.com/kkvinodkumaran/camel

References

1. http://fusesource.com/
2. http://camel.apache.org/selective-consumer.html

Camel Interceptor Simple Example

Apache camel provides intercepting feature while Exchanges are on route. Camel supports three types for interceptors (See more about Camel interceptors )

In this example we will see how to add interceptor in a simple route and that is executing among routes.

1. Create a Maven Project

Create a maven project with following dependencies

   <dependencies>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>2.10.3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-spring</artifactId>
      <version>2.10.3</version>
    </dependency>
    <dependency>
 <groupId>org.apache.activemq</groupId>
 <artifactId>activemq-core</artifactId>
 <version>5.1.0</version>
    </dependency>
    <dependency>
 <groupId>org.apache.activemq</groupId>
 <artifactId>activemq-camel</artifactId>
 <version>5.8.0</version>
</dependency>
        <dependency>
 <groupId>org.apache.activemq</groupId>
 <artifactId>activemq-pool</artifactId>
 <version>5.4.1</version>
</dependency>
    <!-- logging -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.5</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.5</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
  </dependencies>

2. Create a Router

package com.pretech;
import org.apache.camel.builder.RouteBuilder;
/**
 * A Camel Java DSL Router
 */
public class MyRouteBuilder extends RouteBuilder {
 /**
  * Let's configure the Camel routing rules using Java code...
  */
 public void configure() {
  this.intercept().process(new InterceptorProcessor());
  from("file:input?noop=true").process(new ProcessorOne())
    .process(new ProcessorTwo()).process(new ProcessorThree());
 }
}

3. Create Processors


InterceptorProcessor.java

package com.pretech;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class InterceptorProcessor implements Processor  {
 @Override
 public void process(Exchange exchange) throws Exception {
  System.out.println("Interceptor executing");
  
 }
}

ProcessorOne.java

package com.pretech;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class ProcessorOne implements Processor  {
 @Override
 public void process(Exchange exchange) throws Exception {
  System.out.println("Processor one executing");
  
 }
}

Add ProcessorTwo and ProcessorThree as same as above processor


4 Main Program to start Camel (MainApp.java)

package com.pretech;
import org.apache.camel.main.Main;
/**
 * A Camel Application
 */
public class MainApp {
    /**
     * A main() so we can easily run these routing rules in our IDE
     */
    public static void main(String... args) throws Exception {
        Main main = new Main();
        main.enableHangupSupport();
        main.addRouteBuilder(new MyRouteBuilder());
        main.run(args);
     
     
    }
}

Final Structure


image


In this example input folder is starting end point so put some sample input files in input folder and run the main program. We can see interceptor is running among each processors.


Output



Interceptor executing
Processor one executing
Interceptor executing
Processor two executing
Interceptor executing
Processor Three executing


How to compare Arrays contents in Java ?

Arrays.equals(array1, array2) compares the contents of the arrays. See one simple example.

Example

package com.pretech;
import java.util.Arrays;
public class ArrayEqualsExample {
	public static void main(String[] args) {
		String[] array1 = { "Sunday", "Monday", "Tuesday" };
                  String[] array2 = { "January", "February", "March" };
		String[] array3 = { "Sunday", "Monday", "Tuesday" };
        System.out.println("Compare between array1 & array2: " + Arrays.equals(array1, array2));
        System.out.println("Compare between array1 & array3: " + Arrays.equals(array1, array3));
	}
}

Output



Compare between array1 & array2: false
Compare between array1 & array3: true


Confusion Matrix + Precision/Recall (Super Simple, With Examples)

  Confusion Matrix + Precision/Recall (Super Simple, With Examples) 1) Binary Classification Setup Binary classification means the model p...

Featured Posts