Platinum Partner
java,glassfish,ejb 3.1,jndi,@resource

JEE 6 Environmental Enterprise Entries and Glassfish

Purpose

This tutorial is a supplement to the article of oracle published here.

After reading this later, I decided to share some tips about this EE environment configuration.

Introduction

Two years ago, we decided to move to JEE 6 for our enterprise solutions and take a lot of fun with new EJB 3.1 features and annotations. Glassfish has made ​​our lives easier, it was very easy to declare variables using it's administration console.


Less complicated than using ejb-jar.xml are two methods using Glassfish server and resources :

I- Old fashion with resource per param (similar to oracle tutorial)

1- Declare an EJB with toBeInjected resource :
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;

/**
 *
 * @author slim
 */
@Stateless
@LocalBean
public class MyEjbBean {
  @Resource(lookup="resName")
  protected String toBeInjected; 
 
 
  ////
   
}


2 - Open admin console ( localhost:4848 by default ) -> Resources -> Custom Resources -> New -> and fill the informations as below
Glassfish Jndi

At first glance, it looks simple and fast forward but problems start after just after a first project.

  • Problems ! Why ?

We achieved more than 20 variables and some resource names begin to confuse them. I was very hard for our operations team to catch the name of the correct setting and update its value.

It was necessary to group parameters resources into a single resource by project.

Next Fashion : Grouping resources

In this section I will use scala (similar class can be translated easy to java).

1 - First of all, we need to create our single resource class as simple bean

package me.slim.ouertani
class Ressource {
  var param1 : String = _
  var parma2 : String = _ 
}

2 - Next, let's create a resource factory class :

package me.slim.ouertani
class RessourceFactory extends ObjectFactory {
 
  override def getObjectInstance(obj: Object, name: javax.naming.Name, nameCtx: javax.naming.Context, environment: Hashtable[_, _]): Object = {
    val ressource = new Ressource();
    val reference = obj.asInstanceOf[Reference];
    val attributes = reference.getAll();
    while (attributes.hasMoreElements()) {
      val refAddr = attributes.nextElement().asInstanceOf[RefAddr];
      init(ressource, refAddr.getType(), refAddr.getContent());
    }
    return ressource;
  }

  private[this] def init(ressource: Ressource, tipe: String, content: Object) {
    tipe match {  

      case "param1" => ressource.param1 = content.toString()
      case "param2" => ressource.param2 = content.toString()
    
    }
  }
}
The purpose of object factory is to read injectable parameters and populate a resource class

3 - Back to Glassfish admininstration console, create a single resource ( see screenshot below) :
  1. Jndi Name => the resource name to be used by project for example
  2. Resource Type => check second radio button, and fill the resource full name ( with package)
  3. Factory class => Our RessourceFactory class full name ( with package)
  4. Add two (or more) properties to be injected in our resource class.

 

Glassfish JNDI

4- At the end we inject our resource in EJB class :

@Stateless
@LocalBean
class MyEjbBean {
  @Resource(lookup="globalRes")
  protected var res:Ressource= _
 
  /// res.param1
}

Conclusion

Having a single resource per project facilitates business management and thanks to Glassfish administration console managing JNDI became easier.
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}