簡體   English   中英

從容器內部在Websphere中進行EJB查找

[英]EJB lookup in Websphere from within the container

我在Websphere中部署了一個ejb模塊(EAR1),並希望從WAR訪問該EJB,WAR也部署在同一Websphere中。

我嘗試使用下面的代碼,它不起作用。

public class RACAccessProvider {

    private InitialContext myInitialContext;

    public synchronized Object locateEJB(final Class clazz) throws ClassCastException, NamingException {

        try {
            System.out.println("looking up ejb.. for class " + clazz);
            Object obj;
            final String jndiName = clazz.getName();
            obj = myInitialContext.lookup(jndiName);
            System.out.println("###lookuop object.." + obj);
            return obj;
        } catch (final Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public RACAccessProvider() {
        System.out.println("Setting context in RACAccessProvider constructor...");
        final Properties context = new Properties();
        context.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");

        try {
            myInitialContext = new InitialContext(context);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}

上面的代碼掛在一行

obj = myInitialContext.lookup(jndiName);

任何幫助深表感謝。

首先,您需要在上下文對象中設置另一個屬性:

context.setProperty(Context.PROVIDER_URL, "corbaloc::localhost:2809")

其次,您使用的jndi名稱似乎不合適。 應該是這樣的

"java:comp/env/ejb/clazz"
you need to check if your jndi name is correctly mentioned,
Also if initial context is initialized properly as per below code:

InitialContext ic = new InitialContext();
String snName = "java:comp/env/mail/your_jndiname";
Session session = (javax.mail.Session)ic.lookup(snName);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM