如何在同一个 Jvm 中调用 Ejb bean?
How to call Ejb bean in same Jvm?
我想知道在没有 Jax-rs web 的情况下我可以通过多少种方式调用我的 EJb bean service.I 设置 EJB 3 interface/implementation 看起来像这样...
用户服务(接口)
package business;
public interface UserService {
public String doSomething();
}
UserServiceBean(实现)
@Stateless
@Local
public class UserServiceBean implements UserService{
public UserServiceBean() {
}
@Override
public String doSomething() {
return "Work done!";
}
}
我所知道的: 我知道通过调用我的网络服务我可以获得 output : "Work done!"
像这样。
RestService(网络服务)
package webservices;
@Path("/service")
public class RestService {
@Inject
private UserService userService;
public RestService() {
// TODO Auto-generated constructor stub
}
@GET
@Produces(MediaType.TEXT_HTML)
@Path("/userService")
public String getUserServiceResponse(String json){
String response = userService.doSomething();
return response;
}
}
我想要什么:我想要一个简单的method/approach/shortcut等等你可以说。在没有任何 Web 服务的情况下调用我的 EJB bean 以获得我预期的 output : "Work done!"
.
就像我们在 java 中使用 public static void main 方法 application.It 是我有的非常明确的问题 asked.Hope 你们都明白了。
如果我正确理解了您的请求,您正在寻找一种从容器外部从 java main class?
调用 EJB 的方法
在那种情况下你需要
- 为您的容器获取具有正确属性的 JNDI 上下文
- 在容器中查找对 EJB 的引用
- 对其执行业务方法
这是 WildFly 的示例:
public static void main(String[] args) throws NamingException {
Properties jndiProps = new Properties();
jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
jndiProps.put("jboss.naming.client.ejb.context", true);
jndiProps.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
jndiProps.put(Context.SECURITY_PRINCIPAL, "user");
jndiProps.put(Context.SECURITY_CREDENTIALS, "xxx");
Context ctx = new InitialContext(jndiProps);
MyBeanRemote myBean = (MyBeanRemote) ctx.lookup("/<appname>/<beanname>!<fullqualifiedremoteinterface>");
myBean.doSomething();
}
注意以下几点:
- 您的 EJB 需要一个远程接口
- 您可能需要通过脚本在 WildFly 的 bin 文件夹中添加应用程序用户
- 您需要根据需要调整查找字符串,您会在部署期间在 WildFly 日志中看到它(只需省略命名空间前缀)
为了完整起见,我在这里包含了完整的答案,积分属于用户 theisuru
我想知道在没有 Jax-rs web 的情况下我可以通过多少种方式调用我的 EJb bean service.I 设置 EJB 3 interface/implementation 看起来像这样...
用户服务(接口)
package business;
public interface UserService {
public String doSomething();
}
UserServiceBean(实现)
@Stateless
@Local
public class UserServiceBean implements UserService{
public UserServiceBean() {
}
@Override
public String doSomething() {
return "Work done!";
}
}
我所知道的: 我知道通过调用我的网络服务我可以获得 output : "Work done!"
像这样。
RestService(网络服务)
package webservices;
@Path("/service")
public class RestService {
@Inject
private UserService userService;
public RestService() {
// TODO Auto-generated constructor stub
}
@GET
@Produces(MediaType.TEXT_HTML)
@Path("/userService")
public String getUserServiceResponse(String json){
String response = userService.doSomething();
return response;
}
}
我想要什么:我想要一个简单的method/approach/shortcut等等你可以说。在没有任何 Web 服务的情况下调用我的 EJB bean 以获得我预期的 output : "Work done!"
.
就像我们在 java 中使用 public static void main 方法 application.It 是我有的非常明确的问题 asked.Hope 你们都明白了。
如果我正确理解了您的请求,您正在寻找一种从容器外部从 java main class?
调用 EJB 的方法在那种情况下你需要
- 为您的容器获取具有正确属性的 JNDI 上下文
- 在容器中查找对 EJB 的引用
- 对其执行业务方法
这是 WildFly 的示例:
public static void main(String[] args) throws NamingException {
Properties jndiProps = new Properties();
jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
jndiProps.put("jboss.naming.client.ejb.context", true);
jndiProps.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
jndiProps.put(Context.SECURITY_PRINCIPAL, "user");
jndiProps.put(Context.SECURITY_CREDENTIALS, "xxx");
Context ctx = new InitialContext(jndiProps);
MyBeanRemote myBean = (MyBeanRemote) ctx.lookup("/<appname>/<beanname>!<fullqualifiedremoteinterface>");
myBean.doSomething();
}
注意以下几点:
- 您的 EJB 需要一个远程接口
- 您可能需要通过脚本在 WildFly 的 bin 文件夹中添加应用程序用户
- 您需要根据需要调整查找字符串,您会在部署期间在 WildFly 日志中看到它(只需省略命名空间前缀)
为了完整起见,我在这里包含了完整的答案,积分属于用户 theisuru