Archive for the ‘EJB’ Category

JMS with Weblogic Server and EJB MDB

July 15th, 2015 Comments off

Here is a complete and short example of asynchronous development using JMS and a Message-Driven Bean (MDB).
First the procedure to configure inside Weblogic Server a JMS server in order to create a queue, then the code for the producer/sender of a message to the queue and then the code for a consumer (MDB).

1) Weblogic configuration
a) Create the JMS server





b) Create a JMS module








c) Create a destination (queue or topic)





d) Create a connection factory




2) Create a producer

For the producer, I got strongly inspired by the code found in this blog. This is a clean example of a producer/sender so no need to reinvent the wheel here.

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JMSProducer {

	private static InitialContext initialContext = null;
	private static QueueConnectionFactory queueConnectionFactory = null;
	private static QueueConnection queueConnection = null;
	private static QueueSession queueSession = null;
	private static Queue queue = null;
	private static QueueSender queueSender = null;
	private static TextMessage textMessage = null;
	private static final String CONNECTIONFACTORY_NAME = "ConnectionFactory-Test";
	private static final String QUEUE_NAME = "jms/QueueTest";

	public JMSProducer() {

	public static void sendMessageToDestination(String messageText) {
		// create InitialContext
		Hashtable properties = new Hashtable();
		properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
		properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
		properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
		properties.put(Context.SECURITY_CREDENTIALS, "weblogic1");
		try {
			initialContext = new InitialContext(properties);
		} catch (NamingException ne) {
		System.out.println("InitialContext : " + initialContext.toString());
		// create QueueConnectionFactory
		try {
			queueConnectionFactory = (QueueConnectionFactory) initialContext.lookup(CONNECTIONFACTORY_NAME);
		} catch (NamingException ne) {

		System.out.println("QueueConnectionFactory : " + queueConnectionFactory.toString());
		// create QueueConnection
		try {
			queueConnection = queueConnectionFactory.createQueueConnection();
		} catch (JMSException jmse) {

		System.out.println("QueueConnection : " + queueConnection.toString());
		// create QueueSession
		try {
			queueSession = queueConnection.createQueueSession(false, 0);
		} catch (JMSException jmse) {

		System.out.println("QueueSession : " + queueSession.toString());
		// lookup Queue
		try {
			queue = (Queue) initialContext.lookup(QUEUE_NAME);
		} catch (NamingException ne) {

		System.out.println("Queue : " + queue.toString());
		// create QueueSender
		try {
			queueSender = queueSession.createSender(queue);
		} catch (JMSException jmse) {

		System.out.println("QueueSender : " + queueSender.toString());
		// create TextMessage
		try {
			textMessage = queueSession.createTextMessage();
		} catch (JMSException jmse) {

		System.out.println("TextMessage : " + textMessage.toString());
		try {
		} catch (JMSException jmse) {

		System.out.println("TextMessage : " + textMessage.toString());
		// send message
		try {
		} catch (JMSException jmse) {

		System.out.println("Message sent.");
		// clean up
		try {
			textMessage = null;
			queueSender = null;
			queue = null;
			queueSession = null;
			queueConnection = null;
			queueConnectionFactory = null;
			initialContext = null;
		} catch (JMSException jmse) {

		System.out.println("Cleaned up done.");

	public static void main(String args[]) {

3) Create a consumer

This MDB consumer just prints out something in the log when it consumes the message in the queue.
The lifecycle methods are empty but they could be used to clean up resources for instance.

import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJBException;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@MessageDriven(mappedName = "jms/QueueTest", activationConfig = {
		@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") })
public class TestJMSSABean implements MessageDrivenBean, MessageListener {

	/** The Constant LOGGER. */
	protected static final Logger LOGGER = LoggerFactory.getLogger(TestJMSSABean.class);

	public TestJMSSABean() {

	public void onMessage(Message message) {
		try {
			if (message instanceof TextMessage) {
				System.out.println(((TextMessage) message).getText());
				LOGGER.trace("***************** : " + ((TextMessage) message).getText());
			} else {
				LOGGER.trace("***************** : " + message.getJMSMessageID());
		} catch (JMSException ex) {
			LOGGER.trace("***************** ERROR : " + ex.getMessage());

	public void ejbRemove() throws EJBException {
		// TODO Auto-generated method stub

	public void setMessageDrivenContext(MessageDrivenContext arg0) throws EJBException {
		// TODO Auto-generated method stub
Categories: EJB, EJB 3.0, Java EE, WebLogic Server Tags:

Unit testing EJBs with PowerMockito

June 23rd, 2014 No comments

I had to find a way to test a method from a stateful session bean (EJB PersonSABean implements PersonSF) defined in a service layer that calls another method from another stateful session bean (EJB HouseSFBean implements HouseSF) defined in a business layer.

To call that method from EJB HouseSFBean, the EJB PersonSABean needs to perform a lookup to get a reference to the EJB HouseSF. In that case, a static method is often created inside a static or final class. For instance :

public final class EjbUtil {
public static <T> T lookup(final Class<T> clazz) throws SomeException {
 InitialContext context = null;

try {
 context = new InitialContext(new PropertiesLoader().loadProperties("myProperties.txt"));
 return clazz.cast(context.lookup(name));
} catch (NamingException e) {
 throw new SomeException(e.getMessage());
} finally {


@Stateless(name = "PersonSA")
@Remote(value = { PersonSA.class })
public class PersonSABean extends BasicSABean implements PersonSA {
 private HouseSF houseSF;

 public HouseSF getHouseSF() {
    houseSF = EjbUtil.lookup(PersonSF.class, "");
    return houseSF;

But unit tests, unlike integration tests, are meant to test a specific method (or class). They are not meant to test the methods that it calls. So in my case, i do not need  to make – and should not make – a JNDI lookup to get a reference to the EJB HouseSF . So I have to mockup the EjbUtil.lookup(…)  static method.

Fortunately, PowerMockito can mock a static method so the solution is to use both Mockito and PowerMockito :


import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyListOf;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.stub;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

public class PersonSABeanTest {

private HouseSF houseSF;

private PersonSABean personSABean;

public void setUp() throws Exception {
  personSABean = new PersonSABean();

public void testSomeMethod() throws SomeBusinessException {
  SomeDTO someDTO = new SomeDTO();
  stub(houseSF.someMethod(anyListOf(A.class), anyListOf(BDTO.class), any(CDTO.class), anyInt())).toReturn(someDTO);
  Mockito.when(EjbUtil.lookup(anyString(), Mockito.eq(HouseSF.class), anyString())).thenReturn(houseSF);

Link :

Categories: EJB, EJB 3.0, Java EE, PowerMockito, Tests Tags:

Eclipse Dali vs Hibernate Tools

May 8th, 2011 No comments

The process of mapping tables to entities is greatly simplified with tools like Eclipse Dali and Hibernate Tools, both available as Eclipse plugins. It avoids mapping them by hand, which in my opinion is prone to mapping errors and takes more time. And I really do not see why one should map them by hand when great tools like Eclipse Dali and Hibernate Tools are available.
In my book, I describe the use of the Eclipse Dali plugin to automatically generate the entities.
Lately I have also used Hibernate Tools and I have already noticed a few differences between these two tools.
I am going to list some of these differences.
Read more…

Categories: Eclipse, EJB, EJB 3.0, Hibernate, JPA Tags:

Code for the book is now on GitHub

April 3rd, 2011 No comments

I finally added the code for the book “Les EJB 3 (avec Struts 2, JSF 2, JasperReports 3, Flex 3)” to GitHub.

To grab it, you have 2 options :
1) use GIT and type :
git clone git://
2) use SVN and type :
svn co

As a matter of fact, and surprising as it might sound, you can use SVN to grab code from GitHub 🙂

Categories: EJB, EJB 3.0, EJB 3.1, Git, SCM, Subversion Tags:

Enterprise JavaBeans 3.1, sixth edition, available now

October 2nd, 2010 No comments

I just noticed that the sixth edition of the book Enterprise JavaBeans, updated to the specification 3.1, from O’Reilly, by Andrew Lee Rubinger (JBoss) and Bill Burke (JBoss), is now available.
I am looking forward to reading it.
The book does not focus on any specific application server. As mentioned in the introduction :

“Because the focus of this book is on developing vendor independent EJB components and solutions, we have stayed away from proprietary extensions and vendor-dependent idioms wherever possible. You can use any EJBcompliant server with this book, but you should be familiar with your server’s specific installation, deployment, and runtime-management procedures to work with the examples.”

Read more…

Categories: Book reviews, EJB, EJB 3.1 Tags:

My book on EJB 3 is now on sale

July 3rd, 2010 No comments

I received an email from the editor informing me that the book is now printed and will be available in bookstores next week (from July 5th, 2010).

It is already available for sale on Amazon and ENI.

An online version is also available.

Here is the cover of the book :


Autocomplete with jQuery, Struts 2, EJB 3 and JPQL

April 8th, 2010 No comments

If you are looking for a way to fill a field and perform a live search, in an asynchronous way, look no further.
jQuery autocompleter is a jQuery plugin that performs real-time autocompletion of a text-field.

You need jQuery and the jQuery plugin autocomplete of course.

Read more…

Categories: Ajax, EJB 3.0, JPQL, jQuery, Struts 2 Tags:

DAO design pattern in a session bean ?

March 28th, 2010 No comments

I have recently been challenged by a colleague with a .Net background about the way I sometimes use session beans in web applications. Indeed, I like to implement DAOs as session beans.
Some people argue that it’s best not to directly implement CRUD methods in session beans.
The main reason being that they prefer to add another level of separation. For instance :

Session bean client
Session bean
business object helper class
ORM (Hibernate)

A good example of this approach is the old implementation (EJB 2.0) of the The Java Pet Store by Sun. (API :
My opinion is that this level of separation is not always necessary. And I believe using session beans as DAOs makes even more sense now that in EJB 3.x we have the Java Persistence API (entitymanager)  which already provides generic
database access methods (persist(), merge(), etc) :

Session bean client
Session bean DAO (JPA)
Entity bean

Adam Bien explains it well in this post.