Question

I would like to migrate some old EJB 2.1 code to EJB 3.0, but there is some handling of configuration errors in the ejbCreate method. Is there an EJB 3 version of that method?

Edit: In EJB 2.x ejbCreate could throw a CreateException. Based on the documentation of @PostConstruct etc. I can no longer throw any checked Exceptions. How can i handle this if i cannot migrate the code using the EJB right now.

Edit2: The frontend specifically handles CreateException which unfortunately is checked.

Was it helpful?

Solution

@PostConstruct
public void anyName() {
    //initialization code, dependencies are already injected
}

No only the name is arbitrary, you can have several @PostConstruct methods in one EJB - however the order of invocation is unspecified, so be careful and try to stick with one method. UPDATE:

Only one method can be annotated with this annotation.

OTHER TIPS

You need to use EJB 3.0 lifecycle callback methods using annotations

@PostConstruct, @PreDestroy, @PostActivate or @PrePassivate

These annotations can go on any method that is public, void and no-arg.

If the client was explicitly handling CreateException thrown by ejbCreate and you want to use EJB 3, then you must be using a stateful session bean. Exceptions from ejbCreate from stateless session beans are not propagated to clients, and entity beans do not support annotations in EJB 3. In that case, you want the @Init annotation:

public interface MyHome extends EJBLocalHome {
  public MyInterface create(int arg) throws CreateException;
}

@Stateful
@LocalHome(MyHome.class)
public class MyBean {
  @Init
  public void init(int arg) throws CreateException {
    if (arg < 0) {
      throw new CreateException();
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top