Eclipse -clean
Here is a small tip for eclipse users: running eclipse with -clean option once in a while can improve its startup time.
The clean option removes any cached data stored by eclipse runtime and forces cache reinitialization.
Here is a small tip for eclipse users: running eclipse with -clean option once in a while can improve its startup time.
The clean option removes any cached data stored by eclipse runtime and forces cache reinitialization.
The heap size of the Jvm used by Maven (invoked via mvn.bat) can be changed using MAVEN_OPTS parameter at the OS level. For windows, this would be an environment variable.
Variable name: MAVEN_OPTS
Variable value: -Xmx512m
Here is some code to send a message to a JMS queue using Spring:
ConnectionFactory connectionFactory = // Look up connectionfactory from JNDI
Queue queue = // Look up distributed queue from JNDI
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
jmsTemplate.send(queue, new MessageCreator() {
public Message createMessage(Session session) throws JMSException
{
ObjectMessage message = session.createObjectMessage();
// Set the message content
message.setObject(new Object());
return message;
} }
);
… grass grows on your keyboard 🙂
While I was on vaccation, Matt came up with this evil plan to water and grow grass on my keyboard.
It is not uncommon for enterprises to store their employee information in LDAP. Now this poses an interesting challenge for building ORM applications that need access to employee information. For example, consider writing an application that tracks the projects an employee is currently working on. Such an application would have the following simple domain model:
An ORM mapping tool such as Hibernate can easily map this domain model to a database backend. However, since the employee data comes from LDAP, hibernate out of box cannot create the mapping between Project and Employee domain objects. One way to solve this problem would be to implement a custom EntityPersister. In this post I will show a simple hack for achieving this using JPA EntityListeners.
First let us create Java objects and relationships between them. Here is some minimal code:
public class Employee implements Serializable
{
private Long id;
private String firstName;
private String lastName;
// Other fields, getters, setters and other logic
}
public class Project implements Serializable
{
private Long Id;
private String name;
private String description;
private Employee employee;
// Getters, setters and other logic
}
The next step is to define tables in the database. Here is the table structure:
PROJECT
———–
PROJECT_ID | NAME | DESCRIPTION | EMPLOYEE_ID |
EMPLOYEE
—————-
EMPLOYEE_ID |
Since all the employee information (including EMPLOYEE_ID) is available in LDAP, this table simply holds the unique id of an employee.
Then, create mappings for the domain objects using JPA. The mapping for Application is straightforward:
@Entity
@Table(name="PROJECT")
public class Project implements Serializable
{
@Id
@Column(name="PROJECT_ID")
// Sequence generator declarations
private Long Id;
@Column(name="NAME")
private String name;
@Column(name="DESCRIPTION")
private String description;
@ManyToOne(cascade= {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
@JoinColumn(name="EMPLOYEE_ID")
private Employee employee;
// Getters, setters and other logic
}
@Entity
@Table(name="EMPLOYEE")
public class Employee implements Serializable
{
@Id
@GeneratedValue(generator="assigned")
@GenericGenerator(name="assigned", strategy="assigned")
@Column(name="EMPLOYEE_ID")
private Long id;
@Transient
private String firstName;
@Transient
private String lastName;
// Getters, setters and other logic
}
With these mappings in place, hibernate can easily load the Project
object and the corresponding Employee object. To populate the
transient fields in the Employee object, an EntityListener needs to be
created. Here is a simple implementation for the entity listener:
public class EmployeeEntityListener
{
@PostLoad
public void loadEmployee(Employee employee)
{
Map<String, String> employeeData = // Read the employee data from LDAP using the employee Id as the key
employee.setFirstName(employeeData.get("FIRST_NAME"));
employee.setLastName(employeeData.get("LAST_NAME"));
}
}
Finally register this entitylistener:
@Entity
——-
@Table(name="EMPLOYEE")
@EntityListeners(EmployeeEntityListener.class)
public class Employee implements Serializable
{
MDP (Message Driven Pojo) introduced in Spring 2.x offers a nice alternative to EJB’s Message Driven Beans.
package com.inflinx.test.mdp;
import javax.jms.Message;
import javax.jms.MessageListener;
public class TestMdp implements MessageListener
{
public void onMessage(Message message)
{
System.out.println(message);
}
}
Creating and configuring a MDP in Spring is a pretty straight forward process. The first step is to create a class that implements javax.jms.MessageListener interface.
Once the message listener is in place, the next step is to configure the MDP and related JMS components in Spring application context.
<bean id=“testMdp” class=“com.inflinx.test.mdp.TestMdp” />
<jee:jndi-lookup id=“connectionFactory” jndi-name=“jms.testConnectionFactory” />
<jee:jndi-lookup id=“queue” jndi-name=“jms.testQueue” />
<bean id=“jmsTransactionManager” class=“org.springframework.jms.connection.JmsTransactionManager”>
<property name=“connectionFactory” ref=“connectionFactory” />
</bean>
<bean id=“jmsContainer” class=“org.springframework.jms.listener.DefaultMessageListenerContainer”>
<property name=“connectionFactory” ref=“connectionFactory”/>
<property name=“destination” ref=“queue”/>
<property name=“messageListener” ref=“testMdp” />
<property name=“transactionManager” ref=“jmsTransactionManager” />
</bean>
Recently I came across this line of code:
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a");
Unfortunately, this code belongs to a class whose instances will be used by multiple threads. Even though the API documentation for SimpleDateFormat mentions it being not thread safe, somehow it failed to get the developers attention.
As I got to think more about this, a @ThreadSafe JavaDoc element might be useful for API writers to convey their intent more clearly. Perhaps the generated JavaDoc for a thread safe class can use a different color or font making the distinction more visual.