Java 如何在同一Jvm中调用Ejb bean?

Java 如何在同一Jvm中调用Ejb bean?,java,jakarta-ee,ejb,jax-rs,Java,Jakarta Ee,Ejb,Jax Rs,我想知道在没有Jax rs web服务的情况下,我可以通过多少种方式调用EJb bean 用户服务(接口) UserServiceBean(实现) 我知道的:我知道通过调用我的web服务,我可以得到输出:“工作完成!” 像这样 RestService(Web服务) 我想要什么:我想要一个简单的方法/途径/快捷方式等等,你可以这么说。在没有任何web服务的情况下调用我的EJB bean,以获得预期的输出:“工作完成!” 就像我们在java应用程序中使用公共静态void main方法一样。我问的问题

我想知道在没有Jax rs web服务的情况下,我可以通过多少种方式调用EJb bean

用户服务(接口)

UserServiceBean(实现)

我知道的:我知道通过调用我的web服务,我可以得到
输出:“工作完成!”
像这样

RestService(Web服务)

我想要什么:我想要一个简单的方法/途径/快捷方式等等,你可以这么说。在没有任何web服务的情况下调用我的EJB bean以获得预期的
输出:“工作完成!”


就像我们在java应用程序中使用公共静态void main方法一样。我问的问题很清楚。希望你们都明白。

如果我正确理解了你们的请求,你们会想办法从java主类的容器外部调用EJB吗

那样的话,你需要

  • 为容器获取具有正确属性的JNDI上下文
  • 在容器中查找对EJB的引用
  • 在其上执行业务方法
以下是野蝇的一个例子:

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();
    }
publicstaticvoidmain(字符串[]args)引发NamingException{
Properties jndiProps=新属性();
put(Context.INITIAL_Context_FACTORY,“org.jboss.naming.remote.client.InitialContextFactory”);
put(Context.URL_PKG_前缀,“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_主体,“用户”);
jndiProps.put(Context.SECURITY_凭证,“xxx”);
Context ctx=新的初始上下文(jndiProps);

MyBeanRemote myBean=(MyBeanRemote)ctx.lookup(“/

您的问题的主题
如何在同一Jvm中调用Ejb bean
与您的第二句
相矛盾,就像我们在java应用程序中使用公共静态void main方法一样
,因为您询问的是与Ejb在同一Jvm中的服务器中执行的客户机,或者是独立的Ejb客户机(在不同的JVM中运行),所以你的问题一点也不清楚。Steve实际上我不知道Ejb在同一JVM中的正确关系。你能给我解释一下,或者给我链接读出来吗。我只想调用我的Ejb bean类来获得输出。那么你能告诉我怎么做吗?你想从哪里调用你的Ejb?我想从任何类(如Demo)调用我的Ejb。它可以在s中将文件夹命名为Ejb所在的位置。我认为您正在询问如何在不使用JAX-RS的情况下调用
doSomething()
。有很多方法,但我们不知道您希望能够做什么。如果您只想
doSomething()
要在启动时被调用一次,您可以使用
@PostConstruct
进行注释。如果您希望使用计时器自动触发,可以使用
@Schedule
进行注释。您希望如何使用此方法?
@Stateless
@Local
public class UserServiceBean implements UserService{

    public UserServiceBean() {
    }

    @Override
    public String doSomething() {
        return "Work done!";
    }

}
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;
    }

}
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();
    }