如何在JBOSS中获取会话上下文

sdnqo3pr  于 2022-11-08  发布在  其他
关注(0)|答案(3)|浏览(201)

我在比恩的会话中尝试了几种方法,比如:

@Resource
private SessionContext ctx;

private SessionContext ctx;

@Resource
private void setSessionContext(SessionContext ctx) {
  this.sctx = ctx;
}

InitialContext ic = new InitialContext();
SessionContext ctx = (SessionContext) ic.lookup("java:comp/env/sessionContext");

所有这些都不起作用,JBOSS中出现了不同的异常。
我真的很生气。任何人都可以告诉我出了什么问题。非常感谢!

kzipqqlq

kzipqqlq1#

前两种解决方案(现场进样和设定器方法进样)看起来很好,应该起作用
我对第三种方法(查找方法)有疑问,因为您没有显示相应的@Resource(name="sessionContext")注解,但如果使用得当,它也应该工作
第四个选项是查找标准名称java:comp/EJBContext

@Stateless
public class HelloBean implements com.foo.ejb.HelloRemote {
  public void hello() {
    try {
      InitialContext ic = new InitialContext();
      SessionContext sctxLookup = 
          (SessionContext) ic.lookup("java:comp/EJBContext");
      System.out.println("look up EJBContext by standard name: " + sctxLookup);
    } catch (NamingException ex) {
      throw new IllegalStateException(ex);
    }
  }
}

这四种方法都与EJB 3兼容,并且肯定可以与4 Ways to Get EJBContext in EJB 3中提到的任何Java EE 5应用服务器一起使用。如果它们不兼容,请提供您获得的异常的完整堆栈跟踪。

nsc4cvqm

nsc4cvqm2#

您可以使用下面的代码列出这些绑定,它将向您显示上下文中可用的内容。(这使用groovy代码在枚举上执行(each)迭代)

Context initCtx = new InitialContext();
Context context = initCtx.lookup("java:comp") as Context
context.listBindings("").each {
   println it
}

依赖于此代码是在EJB上下文还是Web上下文中运行,您将看到不同的输出。

jdzmm42g

jdzmm42g3#

您可以按如下方式获取SessionContext:

package com.ejb3.tutorial.stateless.client;

import javax.naming.Context;
import javax.naming.InitialContext;

import com.ejb3.tutorial.stateless.clientutility.JNDILookupClass;
import com.ejb3.tutorial.stateless.bean.EmployeeBean;
import com.ejb3.tutorial.stateless.bean.EmployeeServiceRemote;

public class Main {
    public static void main(String[] a) throws Exception {

        EmployeeServiceRemote service = null;
        Context context = JNDILookupClass.getInitialContext();
        String lookupName = getLookupName();
        service = (EmployeeServiceRemote) context.lookup(lookupName);
        service.addBid("userId",1L,0.1);
      }

     private static String getLookupName() {
            /*The app name is the EAR name of the deployed EJB without .ear 
            suffix. Since we haven't deployed the application as a .ear, the app 
            name for us will be an empty string */
            String appName = "SessionContextInjectionEAR";

            /* The module name is the JAR name of the deployed EJB without the 
            .jar suffix.*/
            String moduleName = "SessionContextInjection";

            /* AS7 allows each deployment to have an (optional) distinct name. 
            This can be an empty string if distinct name is not specified.*/
            String distinctName = "";

            // The EJB bean implementation class name
            String beanName = EmployeeBean.class.getSimpleName();

            // Fully qualified remote interface name
            final String interfaceName = EmployeeServiceRemote.class.getName();

            // Create a look up string name
            String name = "ejb:" + appName + "/" + moduleName + "/" + 
                    distinctName    + "/" + beanName + "!" + interfaceName +"?stateless";
            System.out.println("lookupname"+name);
            return name;
        }
}

我的JNDILookupClass如下所示:
公共类JNDILookupClass {私有静态上下文初始上下文;“”“”“”“”“”“”“”“”“”用户名和密码:用户名和密码:在这个例子中,您可以使用一个静态的字符串来表示您的请求。

public static Context getInitialContext() throws NamingException {
        if (initialContext == null) {
            Properties clientProperties = new Properties();
            clientProperties.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
            clientProperties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACES);
            clientProperties.put(Context.PROVIDER_URL, PROVIDER_URL);
            clientProperties.put("jboss.naming.client.ejb.context", true);
            initialContext = new InitialContext(clientProperties);
        }
        return initialContext;
    }

}
我的jboss-ejb-client.properties如下:

endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.password=admin123
remote.connection.default.host=localhost
remote.connection.default.port=8080
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=adminUser

我的Ejb bean如下所示:

package com.ejb3.tutorial.stateless.bean;

import java.util.Date;

import javax.annotation.Resource;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;

/**
 * Session Bean implementation class EmployeeBean
 */
@Stateless
@Local({EmployeeServiceLocal.class})
@Remote({EmployeeServiceRemote.class})
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {

    @Resource
    private SessionContext ctx;

    public EmployeeBean() {
    }

    public Long addBid(String userId, Long itemId, Double bidPrice) {
        System.out.println("Bid for " + itemId + " received with price" + bidPrice);

        TimerService timerService = ctx.getTimerService();
        Timer timer = timerService.createTimer(new Date(new Date().getTime()), "Hello World");
        return 0L;
    }

    @Timeout
    public void handleTimeout(Timer timer) {
      System.out.println(" handleTimeout called.");
      System.out.println("---------------------");
      System.out.println("* Received Timer event: " + timer.getInfo());
      System.out.println("---------------------");

      timer.cancel();
    }

}

我的Ejb接口如下:

package com.ejb3.tutorial.stateless.bean;

import javax.ejb.Remote;

@Remote
public interface EmployeeServiceRemote {
    public Long addBid(String userId,Long itemId,Double bidPrice);
}

相关问题