Showing posts with label EJB3. Show all posts
Showing posts with label EJB3. Show all posts

29.5.12

EJB Lifecycle Interceptor

Depending on the actually type of your Enterprise Java Bean you can have different lifecycle events e.g. PostConstruct or PreDestroy. It is also possible to create Interceptors for these callback methods. The following code shows an EJB with a intercepted callback method and the Interceptor implementation.


@Stateful
@Interceptors({MyInterceptor.class})
public class MemberRegistration{
   
    @PostConstruct
    public void init() {
           // do smg
    }
}

public class MyInterceptor implements Serializable{

    @PostConstruct
    public Object intercept(InvocationContext ctx) throws Exception{
         // do smg
        return ctx.proceed();
    }
}

The first thing to note  is that interceptors for lifecycle events can be just declared at class level; nothing happens if the annotation is added to the method. Furthermore, a general advice, do not forget to call the proceed method on the context object (unless intended). I saw several examples without it, which results in overwriting the intercepted method.

8.1.12

EJB 3.x: One Interface Two Implementations

Note: This entry is about handling the issue "One Interface Two Implementations" for Enterprise Java Beans (EJB) 3.x. Fixing such a problem for the Context Dependency Injection (CDI) framework is done differently and will be covered by another entry.

Problem
If you provide two implementations within your WAR or EAR file for just one interface and inject it using this code

@EJB
private MyInterface myInterface;

the application server will complain as soon as you try to deploy your archive; e.g. Glassfish throws an exception looking something like that:

Cannot resolve reference Remote ejb-ref name=at.rt.ClientBean/myInterface,Remote 3.x interface =at.rt.sameinterface.MyInterface,ejb-link=null,lookup=,mappedName=, jndi-name=,refType=Session because there are 2 ejbs in the application with interface at.rt.sameinterface.MyInterface.

 
Solution
If you want to keep your annotation, simply use the following code to resolve the lookup problem:

@EJB(beanName="MyInterfaceOneBean")
private MyIntterface myInterface;

The other possibility is to modify your deployment descriptor and add an entry similar to that:

<ejb-jar ...>
 <enterprise-beans>
    <session>
        <ejb-name> ClientBean </ejb-name>
        <ejb-local-ref>
            <ejb-ref-name>at.rt.ClientBean/myInterface</ejb-ref-name>
            <local>at.rt.sameinterface.MyInterface </local>
            <ejb-link>MyInterfaceOneBean</ejb-link>
        </ejb-local-ref>
    </session>
</enterprise-beans>
</ejb-jar>

12.12.11

Simple EJB 3.0 Timer

Creating an EJB 3.0 Timer is pretty straight forward.
First, the TimerService is needed. It can be  retrieved direclty via Dependency Injection or using the SessionContext.

@Resource
private javax.ejb.TimerService timerSerivce;

The TimerService interface offers different methods for the creating the actual timer. Most important is the differentiation between  single-event and interval.

public void doSmg() {
     System.out.println("Create Timer");
     timerSerivce.createTimer(3000,3000,  
                            "ObjectAccessibleInTheTimeoutMethod");
}

The first parameter indicates when the timer goes off; the value must be relative, so in this case it will be executed in 3 seconds. For details on the method signatures, please refer to the API doc.
Within the same Bean a method with the following signature and annotation is needed:

@Timeout
 public void timeSomething(Timer timer) {
        System.out.println(timer.getInfo());
 }

It is executed whenever the timer goes off. The getInfo() method returns the passed serializable Object, in this case the String "ObjectAccessibleInTheTimeoutMethod".

Timer Facts:
  • Timers are persistent by specification, if the server crashes or is shutdown, created timers must still exist. However, just to get sure check your server's documentation, e.g. older JBoss servers versions delete timers after  a shutdown. Furthermore, the persisting - implementation is not specified, so this can vary too.
  • If the execution of a timer was missed, because e.g. the server was shut down, it will immediately be called after the server is back online.
  • The timer methods (annotated with @Timeout) are execeuted within a transaction.
  • Possible just for Message Driven and Stateless Session Beans