Well. Let's assume your app is running at some JavaEE/JacartaEE App Server like Payara for example. Isn't it?
First, in my oppinion you do not need to put your proxies to threadlocal, you may rather consider keep Local interface view or even No interface view field in your POJO. And so, you can still use Context lookup.
For example. Your Stateless bean:
package stack.overflow.ejb.module.control;
import javax.ejb.Stateless;
/**
*
* @author s.kadakov
*/
@Stateless
public class SomeBean {
public String getBeanName() {
return SomeBean.class.getCanonicalName();
}
}
Your POJO:
package stack.overflow.ejb.module.control;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
*
* @author s.kadakov
*/
public class SomePojo {
SomeBean someBean = lookupSomeBeanLocal();
public SomeBean getSomeBean() {
return someBean;
}
private SomeBean lookupSomeBeanLocal() {
try {
Context c = new InitialContext();
return (SomeBean) c.lookup("java:global/ejb-module-1.0-SNAPSHOT/SomeBean");
} catch (NamingException ne) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
throw new RuntimeException(ne);
}
}
}
Hint: you may pick up Portable JNDI name of you bean in application server log when your application deployment. In my case those are:
Portable JNDI names for EJB SomeBean: [java:global/ejb-module-1.0-SNAPSHOT/SomeBean, java:global/ejb-module-1.0-SNAPSHOT/SomeBean!stack.overflow.ejb.module.control.SomeBean]]]
Your JAX-RS resource:
package stack.overflow.ejb.module.resources;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import stack.overflow.ejb.module.control.SomePojo;
/**
*
* @author s.kadakov
*/
@Path("javaee8")
public class JavaEE8Resource {
@GET
public Response ping(){
SomePojo pojo = new SomePojo();
String beanName = pojo.getSomeBean().getBeanName();
return Response
.ok(beanName)
.build();
}
}
So, let' try:
curl http://localhost:8080/ejb-module/resources/javaee8
stack.overflow.ejb.module.control.SomeBean
I tested this snippet at Payara 5 Full, hope it would work same way at others JavaEE/JakartaEE compatible servers.
Hope it will be usefull or will provide some hint for you.