Harish Gupta
Harish Gupta

Reputation: 33

Access EJB from normal class

I am using Facade pattern to access by database entities. I have written a wrapper to access the Facade EJB like below. As I understand from the exception, it seems like the EJB is notyet initialized. After reading about the exception on forums, I understood that it should be resolved @PostConstruct notation but still no help. May be I using it wrong, any pointers will be greatly appreciated

public class PatientSearchHelper {
    @EJB
    private PatientFacade patientFacade;

    private final Patient patient;
    private ResponseHeader respHeader;
    private SearchResponse searchResponse;
    private List<Patient> resultSet;


    public PatientSearchHelper (Patient patient) {
        this.patient = patient;
    }

    @PostConstruct
    public void initialize() {
        this.respHeader = new ResponseHeader();
        this.searchResponse = new SearchResponse();
    }

    public SearchResponse getById() {

        System.out.println("Patient Id: " + patient.getPatientid());
        //patientFacade = (PatientFacade) new InitialContext().lookup("java:global/Aarogayam2/PatientFacade!common.facades.PatientFacade");
        resultSet = patientFacade.findById(patient.getPatientid());


        if (resultSet.size() > 0) {
           formatFoundResponse();
        } else {
            formatNotFoundResponse();
        }
        return searchResponse;
    }

    private void formatFoundResponse() {
        searchResponse.setPayload(resultSet);
        respHeader.setSuccess(true);
        searchResponse.setHeader(respHeader);
    }

    private void formatNotFoundResponse() {
        respHeader.setSuccess(false);
        respHeader.setMessage("No Patient found");
        searchResponse.setHeader(respHeader);
        searchResponse.setPayload(null);
    }
}

However I get the exception when calling it like below

PatientSearchHelper searchHelper = new PatientSearchHelper(patient);
searchHelper.initialize();
return searchHelper.getById();

Exception

SEVERE:   java.lang.NullPointerException
    at common.helpers.PatientSearchHelper.getById(PatientSearchHelper.java:48)
    at common.services.PatientService.getById(PatientService.java:57)
    at common.services.PatientService$Proxy$_$$_WeldSubclass.getById(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

Upvotes: 3

Views: 899

Answers (3)

XiCoN JFS
XiCoN JFS

Reputation: 628

Another approach would be to lookup a bean in the Beanmanger with following generic code, which simply returns an instance of your desired class if found:

private static <T> T lookUpClassInBeanManager(Class<T> clazz) {
    BeanManager bm = CDI.current().getBeanManager();
    Bean<T> bean = (Bean<T>) bm.getBeans(clazz).iterator().next();
    CreationalContext<T> ctx = bm.createCreationalContext(bean);
    return (T) bm.getReference(bean, clazz, ctx);
}

public static PatientFacade lookUpPatientFacade() {
        return lookUpClassInBeanManager(PatientFacade.class);
}

This way you can always get an existing instance of your EJB-class. This comes in handy for example in a @FacesConverter or any other class which cannot be declared @Stateless.

Upvotes: 0

AsSiDe
AsSiDe

Reputation: 1846

Instead of making a new Stateless EJB to wrap another one (and if your methods don't require Transactional support), you can use CDI beans using @Named annotation.

@Named
public class PatientSearchHelper {
@EJB
private PatientFacade patientFacade;

private final Patient patient;
private ResponseHeader respHeader;
private SearchResponse searchResponse;
private List<Patient> resultSet;
...

Upvotes: 0

IndoKnight
IndoKnight

Reputation: 1864

If you want to make container to create EJB instances, you need to access using using JNDI or @EJB annotation. In order to make the above code to work make PatientSearchHelper class an EJB and use @EJB in your client code to get the instance before accessing any methods.

Upvotes: 2

Related Questions