Monday, May 25, 2015

Spring Scheduler hello world example - run task in the future

Spring framework now support scheduler, so for most usage there is no need to involve other library, such as Quartz to do this.  The usage is very simple: add annotation @Scheduled(...) to any bean's method that need to be run in the future. The Spring context will create a thread to run it at the right time you specified in the future.

0. What you need

  • JDK 1.7
  • Maven 3.2.1
  • Spring 4.1.0.RELEASE

1. Configure the maven pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<modelversion>4.0.0</modelversion>
<groupid>com.shengwang.demo</groupid>
<artifactid>spring-scheduler-hello-world</artifactid>
<version>0.0.1-SNAPSHOT</version>

<dependencies>

<!-- Spring Context aritifact-->
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context</artifactid>
<version>4.1.0.RELEASE</version>
</dependency>

</dependencies>

<!-- Using JDK 1.7 for compiling -->
<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>


2. Define the Java Class



In the example there are 2 classes. The first one is a simple spring bean, but with annotation @Scheduled for method.

package com.shengwang.demo;

import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class Task {
/*
* The method will run on 13:40:00 everyday.
* cron format: sec min hour day mon week
*/
@Scheduled(cron="0 40 13 * * *")
public void runTask() {
System.out.println("Runing at " + new Date());
}
}


The second class is the main class.

package com.shengwang.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoMain {

public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("app-context.xml");
}
}
The only line in the main is used to create the spring application context. If you run the main function, the program will not never finish since the spring context is not closed. so it will wait for the scheduled task to run in the future. The app-context.xml is the spring configuration file.

 



3. Spring configuration



The spring configuration file is named "app-context.xml" here. It stays in the main resources path /src/main/resources.

<?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:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
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://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">

<!-- enable scheduling annotations -->
<task:annotation-driven scheduler="scheduler"/>
<task:scheduler id="scheduler" pool-size="15"/>

<!-- register spring beans -->
<context:component-scan base-package="com.shengwang.demo" />

</beans>


There are 2 lines start with <task:... /> used to configure spring scheduling. One thing need to mention is that (from spring official document):  If you do not provide a pool-size attribute, the default thread pool will only have a single thread. So in real practical, the pool-size is should always be set  to a number bigger than one to prevent any long task delaying all other scheduled tasks.

Monday, May 18, 2015

Understand all about hibernate fetch - WHEN and HOW

As Hibernate official document says:

Hibernate have two orthogonal notions here: when is the association fetched and how is it fetched.

There are many words on different document to describe ORM fetch like fetch strategy, fetch type, fetch mode, but in essence just the concept of WHEN and HOW.

In this article, a One-To-Many example will be used to demonstrate the concept.  A Client entity and a PurchaseOrder Entity are used in the demo code. One client can have many purchase orders. There are 5 clients in database and every client has 10 orders.

image

0. Environment and version

  • Java 7 (JPA2.0)
  • Hibernate 4.x

The following example and discuss are based on this environment.

1. Concept in short

1.1 WHEN to fetch

  • Eager : Assemble SQL and access database immediately when an entity is created. JPA compatible
  • Lazy  -  A wrapper class called 'proxy' is used for the entity, when an entity is created. The database access only happens when entity's value gets used. JPA compatible. Default behavior. 
  • Extra Lazy  -  Try not to fetch the whole collection into memory unless absolutely needed. Not JPA compatible

When means at what time will hibernate assemble a SQL clause to access the database.

1.2 How to fetch

If a entity have associations, how the associations of the entity get loaded.  In our Client-To-PurchaseOrder(One-To-Many) demo, when a client entity is loaded, how to get his purchase orders. 

  • Join  - Use SQL outer join to get associations. Be careful about the return list, it may not like what you expect,  see the demo below.   JPA compatible. Default behavior for Eager.
  • Select  - Use a separate select to get only 1 entity's association,  will cause the 'N+1' problem.   JPA compatible. Default behavior FOR Lazy.  
  • Batch  -  Use a separate select to get a batch size entities' association. Can be treated as a kind of improvement of 'Select' Not JPA compatible
  • Subselect  -  Use a separate select to get all associations for all entities retrieved in a previous query. Not JPA compatible

JPA compatible means in java code, only classes or annotations from javax.persistence.* are used, no need to import any thing  from  org.hibernate.*.  Hibernate can just be used as persistence provider in JPA configure file persistence.xml, which makes the the application not close coupled with Hibernate.

2. Code for demo

2.1 The Entity definitions

The entity PurchaseOrder is plain. Nothing's tricky here.

package com.shengwang.demo.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;


@Entity
@Table(name="purchase_order")
public class PurchaseOrder {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="order_id")
private int orderId;

@Column(name="order_desc")
private String orderDesc;

@ManyToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="client_id")
private Client client;

public int getOrderId() {
return orderId;
}

public String getOrderDesc() {
return orderDesc;
}


public Client getClient() {
return client;
}

public void setOrderId(int orderId) {
this.orderId = orderId;
}

public void setOrderDesc(String orderDesc) {
this.orderDesc = orderDesc;
}

public void setClient(Client client) {
this.client = client;
}
}

All magic happens in the Entity Client. which has a Set variable orders associated with PurcharseOrder to represent the one-to-many mapping. We will change the annoatation for Set variable orders to get different fetch when-how combinations.

package com.shengwang.demo.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="client")
public class Client {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="client_id")
private int clientId;

@Column(name="client_name")
private String clientName;

//---------------------------------
// This is where the magic happens
//---------------------------------
@OneToMany(fetch=FetchType.LAZY,mappedBy="client")
private Set<PurchaseOrder> orders = new HashSet<PurchaseOrder>();


public int getClientId() {
return clientId;
}
public String getClientName() {
return clientName;
}

public Set<PurchaseOrder> getPurchaseOrders() {
return orders;
}
public void setClientId(int clientId) {
this.clientId = clientId;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public void setPurchaseOrders(Set<PurchaseOrder> orders) {
this.orders = orders;
}
}

2.2 demo code snippet


Following code snippet is used to show how the different fetch combinations effect. We'll keep running this code on different when-how fetch combinations to demonstrate differents effects

@Override
@Transactional
public void printOrdersForEveryClient() {
List <Client> clients;
Criteria criteria;
criteria = sessionFactory.getCurrentSession().createCriteria(Client.class);
clients = criteria.list(); // checkpoint-1
System.out.println("clients.size="+clients.size());

for (Client client: clients) {
System.out.println("client : "+ client.getClientId());
System.out.println(""+client.getPurchaseOrders().size()); // checkpoint-2
Set <PurchaseOrder> orders = client.getPurchaseOrders(); // checkpoint-3
for (PurchaseOrder order : orders) {
System.out.println(order.getOrderId()+","+order.getOrderDesc());
}
}
}

The method printOrdersForEveryClient() is supposed to print out purchase orders for all clients one by one. The comments checkpoint1-4 is added as anchors to locate where the database accessing happens.


3. Behaviors of When+How fetch combinations


Let's try different When-How fetch combinations on the one-to-many(client-to-purchase order) mapping,  see what really happens. All the following SQL clauses are NOT the original Hibernate generated ones, but simplified to make them more understandable.


3.1 Eager + Join


Changing the definition of Client entity, set when to eager by "fetch=FetchType.EAGER", set how to join by @Fetch(FetchMode.JOIN). Since join is the default behavior for eager, annotation  @Fetch(FetchMode.JOIN) can be omitted.

@Entity
@Table(name="client")
public class Client {
// omit other fields

//---------------------------------
// This is where the magic happens
//---------------------------------
@OneToMany(fetch=FetchType.EAGER,mappedBy="client",cascade=CascadeType.PERSIST)
private Set<PurchaseOrder> orders = new HashSet<PurchaseOrder>();

// omit getters & setters
}

The database accessing happens only in 1 place:
select a.*,  b.* from client a left outer join purchase_order b on a.client_id=b.client_id      at checkpoint-1


Suppose there are 5 rows in client table and every client have 10 purchase orders in purchase_order table. The result list size at checkpoint-1 is not 5, but 5x10 = 50. This will make the method printOrdersForEveryClient() doesn't work as design. 


3.2 Lazy + Select (default behavior)


The default when is lazy, and the default how for lazy is select.


The database accessing happens in  2 places:
select *  from client   at checkpoint-1
select * from purchase_order  where client_id=?     at checkpoint-2


Since the checkpoint-2 is in the for loop, every loop will issue a sql query to database, perfectly demonstrate the so-called 'N+1' problems.
You may also notice that although lazy fetch is used, the database accessing immediately happens when querying the clients.  That's because the lazy fetch only works for query entity by primary key.


3.3 Eager+ Select


Changing the definition of Client entity, set when to eager by "fetch=FetchType.EAGER", set how to join by @Fetch(FetchMode.SELECT)

@Entity
@Table(name="client")
public class Client {
// omit other fields

//---------------------------------
// This is where the magic happens
//---------------------------------
@OneToMany(fetch=FetchType.EAGER,mappedBy="client",cascade=CascadeType.PERSIST)
@Fetch(FetchMode.SELECT)
private Set<PurchaseOrder> orders = new HashSet<PurchaseOrder>();

// omit getters & setters
}

The database accessing happens only in 1 place, but N+1 SQL clauses:


select * from client                                                                             at checkpoint-1
select * from purchase_order where client_id=?                at checkpoint-1               
select * from purchase_order where client_id=?               
at checkpoint-1
select * from purchase_order where client_id=?                at checkpoint-1
select * from purchase_order where client_id=?                at checkpoint-1
select * from purchase_order where client_id=?                at checkpoint-1

The query in java also creates N+1 SQL clauses, but all at same time. One to get all clients and 5 to get orders for each client.  (There are 5 clients in the client table) 


3.4 Lazy + Batch


Changing the definition of Client entity, set when to lazy by "fetch=FetchType.LAZY", set how to batch by @Fetch(FetchMode.SELECT) and @BatchSize(size=3) . Since select is the default behavior for lazy, annotation  @Fetch(FetchMode.SELECT) can be omitted.

@Entity
@Table(name="client")
public class Client {
// omit other fields

//---------------------------------
// This is where the magic happens
//---------------------------------
@OneToMany(fetch=FetchType.LAZY,mappedBy="client",cascade=CascadeType.PERSIST)
@BatchSize(size=3)
private Set<PurchaseOrder> orders = new HashSet<PurchaseOrder>();

// omit getters & setters
}

The database accessing happens in 2 places:
select * from client                                                                             at checkpoint-1
select * from purchase_order where client_id in (?, ?, ?)             at checkpoint-2               


Unlike the select, which accesses database N+1 times,  batch fetch accesses database (N/batchSize +1 ) times. In our demo, there are 5 clients in the table,  batch size is 3, total database accessing counts = celling(5/3)+1=3. 


3.5 Eager + Batch


Changing the definition of Client entity, set when to eager by "fetch=FetchType.EAGER", set how to batch by @Fetch(FetchMode.SELECT) and @BatchSize(size=3)

@Entity
@Table(name="client")
public class Client {
// omit other fields

//---------------------------------
// This is where the magic happens
//---------------------------------
@OneToMany(fetch=FetchType.LAZY,mappedBy="client",cascade=CascadeType.PERSIST)
@Fetch(FetchMode.SELECT)
@BatchSize(size=3)
private Set<PurchaseOrder> orders = new HashSet<PurchaseOrder>();

// omit getters & setters
}

The database accessing happens in 1 place:
select * from client                                                                             at checkpoint-1
select * from purchase_order where client_id in (?, ?, ?)             at checkpoint-1               


Compare to Lazy+Batch, Eager+Batch  create same amount SQL and exactly same SQL clauses. but will no wait for the first time accessing contents of client entity, but immediately load all clients' associations when query client entity. 


3.6 Lazy + Subselect


Changing the definition of Client entity, set when to lazy by "fetch=FetchType.LAZY", set how to subselect by @Fetch(FetchMode.SUBSELECT)

@Entity
@Table(name="client")
public class Client {
// omit other fields

//---------------------------------
// This is where the magic happens
//---------------------------------
@OneToMany(fetch=FetchType.LAZY,mappedBy="client",cascade=CascadeType.PERSIST)
@Fetch(FetchMode.SUBSELECT)
private Set<PurchaseOrder> orders = new HashSet<PurchaseOrder>();

// omit getters & setters
}

The database accessing happens in 2 places:
select * from client                                                                             at checkpoint-1
select a.* from purchase_order a where a.client_id in (select b.client_id from client b)        at checkpoint-2


Unlike the select(N+1) or batch (N/batchsize+1), subselect only access database once to get all associations for the previous query.  The first time in the loop cause the second SQL  and rest of the loop won't access database anymore.  Totally the code accesses database twice.


3.7 Eager+ Subselect


Changing the definition of Client entity, set when to eager by "fetch=FetchType.EAGER", set how to subselect by @Fetch(FetchMode.SUBSELECT)

@Entity
@Table(name="client")
public class Client {
// omit other fields

//---------------------------------
// This is where the magic happens
//---------------------------------
@OneToMany(fetch=FetchType.EAGER,mappedBy="client",cascade=CascadeType.PERSIST)
@Fetch(FetchMode.SUBSELECT)
private Set<PurchaseOrder> orders = new HashSet<PurchaseOrder>();

// omit getters & setters
}

The database accessing happens in 1 place:
select * from client                                                                             at checkpoint-1
select a.* from purchase_order a where a.client_id in (select b.client_id from client b)        at checkpoint-1


Compare to Lazy+Subselect, Eager+Subselect  create same amount SQL and exactly same SQL clauses. but will no wait for the first time accessing contents of client entity, but immediately load all clients' associations when query client entity.


3.8 Extra Lazy+ Batch


Changing the definition of Client entity, set when to extra lazy by "fetch=FetchType.LAZY" and @LazyCollection(LazyCollectionOption.EXTRA), set how to batch by @Fetch(FetchMode.SUBSELECT)  and @BatchSize(size=3)

@Entity
@Table(name="client")
public class Client {
// omit other fields

//---------------------------------
// This is where the magic happens
//---------------------------------
@OneToMany(fetch=FetchType.LAZY,mappedBy="client",cascade=CascadeType.PERSIST)
@LazyCollection(LazyCollectionOption.EXTRA)
@Fetch(FetchMode.SELECT)
@BatchSize(size=3)
private Set<PurchaseOrder> orders = new HashSet<PurchaseOrder>();

// omit getters & setters
}

The database accessing happens in 3 places:
select * from client                                                                             at checkpoint-1
select count(order_id) from purchase_order where client_id= ?                         at checkpoint-2
select * from purchase_order where client_id in (?, ?, ?)              at checkpoint-3               


Compare to Lazy+Batch, Extra Lazy+Batch  create one more SQL clause to avoid getting  all collections contents back when possible.  Extra Lazy is not supported by other persistence providers like EclipseLink. In EclipseLink developer has to manually define a query to get the collection's size without get all contents back.


4. More to notice


The batch size doesn't mean how many rows will be fetched from the associations, but means how many associations to be fetched.  For example using batch fetch,  if there are 1000 purchase orders for each client, batch size = 3, every batch SQL will fetch 3 clients' associations, totally 3000 rows, from purchase_order table.  If only part of the collections are needed, pagination is the solution.

Thursday, January 29, 2015

Using Netty Component in Apache Camel - Hello World Example

This tutorial will demo how to use Netty Component in Apache Camel. By using netty component with camel in the example, this article is going to achieve these goals:

  • Create a TCP Socket server as camel endpoint with netty component, waiting for TCP client.
  • Utilize camel to route the received TCP message from Netty endpoint to other process logic
  • Send the processed result back to the TCP client.

In this example, every time server get a string, it will reply with a prefix "Hello" to form a greeting. If "Tom" is received from client, "Hello Tom" will be send back to the client.

This example also shows there features:

  • Use Camel and Netty component with Spring framework.
  • Use POJO class for business logic, which does not depend on any Camel or Netty componenent APIs.

1. What you need

  • JDK 1.7+
  • Maven 3.2.1

The dependencies will be managed by Maven.

2. Configure the maven pom.xml

The key artifacts are camel-core, camel-spring, camel-netty. The camel-spring artifact is needed for using Spring framework. The camel-netty artifact is needed for using netty component in camel.

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.shengwang.example</groupId>
<artifactId>spring-camel-netty3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-camel-netty</name>
<url>http://maven.apache.org</url>

<properties>
<!-- Generic properties -->
<java.version>1.7</java.version>

<!-- Camel -->
<camel.version>2.14.1</camel.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-netty</artifactId>
<version>${camel.version}</version>
</dependency>
</dependencies>


<build>
<plugins>
<!-- Use JDK 1.7 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>

<!-- Allows the routes to be run via 'mvn camel:run' -->
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>${camel.version}</version>
</plugin>

</plugins>
</build>
</project>

In the POM file, Jave version and Camel version are defined in properties, followed by 3 dependencies. Then there are 2 plugins, the first one is to specify the Java version for building the project, the second plugin is needed to allow the camel route to be run without explicitly defined main function. It's very handy for demo and test.


3. Define a Java class to process the received message


This class is the process logic in this demo. This class is a POJO which means the business code is completely decoupled with camel or camel-netty component.

package com.shengwang.example.server;

import org.springframework.stereotype.Service;

@Service
public class EchoService {
public String sayHello(String guestName) {
System.out.println("Input guestName : "+ guestName);
return "Hello " + guestName;
}
}

The EchoService.java is very short and simple. Annotation @Service means it's will be registered in Spring context as a bean. How to use this bean will be explained later.


4. Define Camel route


Now Let's define another Java class for Camel route. This class extends RouteBuilder from Camel package and override the configure() method. In configure(), route is defined.

package com.shengwang.example.server;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.Main;

public class ServerRoute extends RouteBuilder {
public static void main(String[] args) throws Exception {
new Main().run(args);
}

@Override
public void configure() throws Exception {
from("netty:tcp://localhost:7000?sync=true&allowDefaultCodec=false&encoder=#stringEncoder&decoder=#stringDecoder")
.to("bean:echoService");
}
}

There are two methods in the ServerRoute class. The first one is a main, so we can run the route directly with the helper class Main from camel-spring package.


The second method has the route defined. It means somehow like "from to ". In this tutorial we get message from netty endpoint and send to a Spring bean which defined in previous section. The is how the EchoService class is used in this demo. The interesting part is the long from URI:"netty:tcp://localhost:7000?sync=true&allowDefaultCodec=false&encoder=#stringEncoder&decoder=#stringDecoder". It can be break down bit by bit.


"netty:tcp://localhost:7000" means netty component is needed to setup a tcp server on localhost port 7000.


"sync=true" means exchange pattern is InOut, the server will send back the response to client.


"allowDefaultCodec=false" means we don't use default encoder and decoder for netty component. Because in reality, the message come in netty endpoint is most unlikely formed in that message format, so it need to be set to false against its default value true.


"encoder=#stringEncoder&decoder=#stringDecoder" means message comes into netty will be decoded before transferring to the business logic EchoService, and response from EchoService will be encoded before sending back to the client. In this tutorial, server receive and send back Java string, so the StringEncoder and StringDecoder from camel-netty package will be used. The hash tag "#" refer to beans in Spring context which will be defined in Spring configuration file later.


5. Spring configuration


The Spring configuration file app-context.xml is located in directory src/main/resources/META-INF/spring.

<?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:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:camel="http://camel.apache.org/schema/spring"
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">

<context:component-scan base-package="com.shengwang.example" />

<!-- ================================== -->
<!-- Define camel context -->
<!-- ================================== -->
<camel:camelContext id="camel">
<!-- Location for route scan -->
<camel:package>com.shengwang.example.server</camel:package>
</camel:camelContext>

<!-- ================================== -->
<!-- Define netty codecs -->
<!-- ================================== -->
<!-- ChannelBuffer/ByteBuf to String -->
<bean id="stringDecoder" class="org.jboss.netty.handler.codec.string.StringDecoder"/>

<!-- String to ChannelBuffer/ByteBuf -->
<bean id="stringEncoder" class="org.jboss.netty.handler.codec.string.StringEncoder"/>

</beans>

<context:component-scan> scan for normal Spring beans of a package. Here is the EchoServer. Then,<camel:camelContext>, Camel context is defined so does the location of the route. Finally, encoder and decoder beans are defined.


Now all 4 files of the project have been defined. The directory structure looks like this.


Snap_2015.01.27 13.29.23_001


6. Run the server


There are 2 options to run this demo. The first one is to run the main in ServerRouter.java class. The Second is run via command line "mvn camel:run".  The latter way, which needs a maven plugin list at the bottom of the pom file,  is very convenient for test and development.


7. Test with a TCP client


Use telnet command as test client, connect to port 7000 with command "telnet localhost 7000". Anytime a key is pressed, a TCP message will send out to the demo server  and a reply will come back immediately.


Appendix


The netty component camel-netty is based on netty 3.x version. If a netty component based on netty 4.x is preferred, use camel-netty4 component instead.

Sunday, December 21, 2014

The Value of Annotation @Temporal for Date Related Columns

The annotation @Temporal in official javadoc is described like below:
This annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar.

So when the field variable is a java.util.Date or java.util.Calendar, it MUST be annotated by @Temporal.

There are 3 values for @Temporal

  • TemporalType.DATE
  • TemporalType.TIME
  • TemporalType.TIMESTAMP

Here is a demo to show the usage of there 3 values. The Entity 'MyEntity' has a field called 'lastUpdateTime' which is a defined as a java.util.Date variable. let's update it in java like this:

  import java.util.Date;
//...
// MyEntity is the JPA entity, em is EntityManager context
MyEntity e = em.find(MyEntity.class, 1L); //get the row with id=1
e.setLastUpdateTime(new Date());

TemporalType.DATE


If the column annotated with @Temporal(TemporalType.DATE) like this:

  @Temporal(TemporalType.DATE)
@Column(name="LAST_UPDATE_TIME")
private Date lastUpdateTime;

The record in database after update will look like:Snap_2014.12.22 12.49.51_001


TemporalType.TIME


If the column annotatedwith @Temporal(TemporalType.TIME) like this:

  @Temporal(TemporalType.TIME)
@Column(name="LAST_UPDATE_TIME")
private Date lastUpdateTime;

The record in database after update will look like:Snap_2014.12.22 13.06.58_002


TemporalType.TIMESTAMP


If the column annotated with @Temporal(TemporalType.TIMESTAMP) like this:

  @Temporal(TemporalType.TIMESTAMP)
@Column(name="LAST_UPDATE_TIME")
private Date lastUpdateTime;

The record in database after update will look like:Snap_2014.12.22 13.10.14_003

Thursday, December 18, 2014

How to configure primary key's generator in JPA entity for oracle

In JPA there are 4 strategies to automatically generate value for a primary key column:

  • Auto
  • Identity
  • Sequence
  • Table

To some extent, there are only 3 strategies, the last 3 ones. Because the first 'Auto' just let the JPA implementation to choose which one of the rest 3 strategies it will use. For example working with Oracle + EclipseLink 2.5.0,  the "Auto" will result in the strategy "Table" being choosen . This behavior depends on the database and the JPA implementation in your project.

0. Before we start

In this tutorial, Oracle is database and EclipseLink is JPA implementation.  The table name in this demo is T_JUST_FOR_TEST, which only have 1 field 'id' as its primary key for simplicity's sake.

In JPA the annotation for value generation of  primary key is @GeneratedValue

Note : The value generation strategy only  take effect when you try to write back to database from your java application. In other words, if the table is read-only in the Java application,  Any specified strategy is meaningless in Entity class.

1. Use Strategy Identity

Oracle don't support Identity.

2. Use Strategy Sequence

2. 1 Define Sequence in Database

Make sure there is sequence that you can use or you can define a new one like this.

-- Create sequence 
create sequence SEQ_JUST_FOR_TEST
minvalue 1
maxvalue 9999999999999999999999999999
start with 1
increment by 1;

The created sequence is named 'SEQ_JUST_FOR_TEST'. it starts from 1 and increase 1 for every next value.


2.2 Define Entity class


Use Annotations for primary key column. First define a sequence generator, then use the generator.

package com.shengwang.example;

import java.io.Serializable;
import javax.persistence.*;

@Entity
@Table(name="T_JUST_FOR_TEST")
public class TJustForTest implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@SequenceGenerator(name="SEQ_GEN", sequenceName="SEQ_JUST_FOR_TEST", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_GEN")
private long id;

public TJustForTest() {}

public long getId() {
return this.id;
}

public void setId(long id) {
this.id = id;
}
}

In the above Entity definition class, The annotation @SequenceGenerator is used to define a Sequence Generator named "SEQ_GEN", which is then used in the @GeneratedValue annotation.  The attribute sequenceName is the real sequence  name in Oracle. The allocationSize is the increment for every step.  Make sure the value of allocationSize matches the corresponding value in the sequence definition in Oracle.  In the annotation @GeneratedValue, strategy "GenerationType.SEQUENCE" is specified, and generator set to the sequence generator "SEQ_GEN" defined one line above.


Every time when insert a new TJustForTest entity record into the table like below, the primary key id will increase one.

  // em is the injected EntityManager;
TJustForTest t = new TJustForTest();
em.persist(t);

3. Use Strategy Table


3.1 Define table in database


A table need to be defined in database.

create table T_GENERATOR1
(
gen_key VARCHAR2(255) not null,
gen_value NUMBER,
constraint pk_gen_key PRIMARY KEY (gen_key)
)

The table works somehow like a key-value pair or hash table. Every row of the table can  logically be treated as a sequence.  Normally, the data type of key column is string and data type of value column is number. To make it more readable, A string combines schema(MONITOR2012) and table name(T_JUST_FOR_TEST) is used as key in this tutorial.


3.2 Insert one row into the table

insert into T_GENERATOR (Gen_Key,GEN_VALUE) values ('MONITOR2012.T_JUST_FOR_TEST',0)

This one row data has to be insert in to the table T_GENERATOR before we can use it as generator in JPA entity class.


3.3 Define Entity Class


Use Annotations for primary key column. First define a table generator, then use the generator.

package com.shengwang.example;

import java.io.Serializable;
import javax.persistence.*;

@Entity
@Table(name = "T_JUST_FOR_TEST")
public class TJustForTest implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@TableGenerator(name="TABLE_GEN",table="T_GENERATOR", pkColumnName = "GEN_KEY", pkColumnValue = "MONITOR2012.T_JUST_FOR_TEST", valueColumnName = "GEN_VALUE", initialValue = 1, allocationSize = 1 )
@GeneratedValue(strategy = GenerationType.TABLE, generator="TABLE_GEN")
private long id;

public TJustForTest() {
}

public long getId() {
return this.id;
}

public void setId(long id) {
this.id = id;
}
}

The annotation @TableGenerator is used to define a table generator named "TABLE_GEN". This table generator is used as the generator for @GeneratedValue.


Let's go through all the rest attributes in annotation @TableGenerator in details.


table is the table name in database used. pkColumnName is the column name of the table.. pkColumnValue is the value of the key. These 2 attributes are logically combined as a where phase in SQL, " Select ... from T_GENERATOR where GEN_KEY='MONITOR2012.T_JUST_FOR_TEST'", to locate the single record in the database table. valueColumnName is the column name to store real id value. initialValue is the first value of the generator. allocationSize is the step to increase. 


This is how these attributes work in logic: Every time JPA implementation need to generate value, it will first find the table table, then find the row using pkColumnName and pkColumnValue, then read the value of valueColumnName, add allocationSize, use it and update the value back to valueColumnName.


Every time when insert a new TJustForTest entity record into the table like below, the primary key id will increase one.

  // em is the injected EntityManager;
TJustForTest t = new TJustForTest();
em.persist(t);

4. Summary

Strategy sequence and strategy table can be used in JPA entity class if working with Oracle. The strategy sequence is oracle dependent, it does not work if you change to other database like MySQL. The Strategy table is database independent, but seems a little bit more complex than sequence strategy.

Tuesday, December 9, 2014

How to create Java classes from existing tables in database for JPA

If JPA is your persistence layer in your project and you have tables already defined in database. You can use JPA tools in Eclipse to automatically create corresponding Java Entity classes from those tables in database.

In this article, Java JPA entities will be created from an oracle database on LAN.

1. what you need

  • Eclipse 4.3 +  ( In this article, Eclipse Java EE version 4.3.2 kepler is used )
  • Database connection configuration in Eclipse. In eclipse, configure a database connection to the oracle database.  If you don't know how to do that, read this article first How to add oracle DB connection in Eclipse.

2. Make sure you have JPA support enabled in your projects

Righ click on the project name and select "Properties"

Snap_2014.12.09 14.30.34_003

Use project facets if project facets not enabled

Snap_2014.12.09 10.02.48_001

Click the "JPA" if it's not selected, then click "OK" to close the project properties window.

Snap_2014.12.09 14.34.20_004

3. create Java classes from tables

After enabling JPA in project properties, now right click you eclipse project name again, you should see a new context menu item "JPA tools" appears. Choose "Generate Entities from tables"

If you see the "JPA tools" menu but the sub menu don't have "Generate Entities from tables"  item, make sure you right click on the project name instead of a package or a class file.

Snap_2014.12.09 14.49.33_006 

Select a database connection to let Eclipse get the tables  used to generated class from.  (See another tutorial on  how to configure a database connection in Eclipse)

Snap_2014.12.09 15.04.08_007

After connected to the database, schema and tables will be listed below, choose the tables you need in your Java project and click "Next"Snap_2014.12.09 15.07.21_008

In this tutorial  2 tables are selected. There is a  foreign key between 2 tables. Logically one  platform can have more than one devices. It's a "1 to many" mapping.

Next Eclipse automatically create associations between tables you select.  Click each association to see the details.

Snap_2014.12.09 15.20.46_009

In the final step, Eclipse offers the chance to change generated Java class name and the field name inside the generated java class.

Snap_2014.12.09 15.28.13_011

Snap_2014.12.09 15.29.02_012

Click "finish" and the Java classes will be created.  For example one of the Entity class created looks like:

Snap_2014.12.09 15.38.05_013

The created Java Entity class is well annotated and ready for use!

Monday, December 8, 2014

How to add oracle DB connection in Eclipse

Have a DB connection in Eclipse will be very helpful if you need use persistence frameworks like Hibernate or JPA.  It's normally a prerequisite for other functions, for example let eclipse create java entities class from database tables automatically.  Here's how to configure oracle connection in Eclipse.

1. what you need

  • Oracle JDBC driver. ( A jar file may look like ojdbcxxx.jar, if you don't have it on your local file system, download it from oracle website)

2. open "Data Source Explorer" view in Eclipse

From menu, select "window"->"Show View"->"Other"

Snap_2014.12.09 11.23.04_014

Next, Choose "Data Source Explorer" from category "Data Management"

Snap_2014.12.09 10.04.24_002

Then you should have the view opened.

3. Add new connection

Right click on the "Database Connection" in the "Data Source Explorer" view we just opened.  choose "New..."

Snap_2014.12.09 10.06.49_003 

Choose database type ( here is Oracle), give a name for this connection (here is "oracle")

Snap_2014.12.09 10.08.52_005

Next, select the oracle driver for this connection by click the follow icon pointed by the arrow.

Snap_2014.12.09 10.13.26_006

Next, choose oracle version (I'm using oracle 11 for this demo, you can pick up your version) ,  and click the "Jar List" tab to set the driver file path

Snap_2014.12.09 10.14.51_007

In the "Jar List" tab, replace the default non-exists oracle driver file, which is ojdbc14.jar, with the real one  in you local file system, which is d:\ojdbc6.jar

Snap_2014.12.09 10.17.27_009

Snap_2014.12.09 10.19.15_010 

Close the above window,  next provide connection information.

Snap_2014.12.09 10.22.01_012

Use "Test Connection" button to make sure every thing is OK, then Finish.

4. After Connection created

You can see the new oracle DB connection appears in the "Data Source Explorer" view.

Snap_2014.12.09 11.50.04_015

You can browse the database in the view within Eclipse.

Powered by Blogger.

About The Author

My Photo
Has been a senior software developer, project manager for 10+ years. Dedicate himself to Alcatel-Lucent and China Telecom for delivering software solutions.

Pages

Unordered List