hyda
hyda

Reputation: 570

Get Stateless Bean from InitialContext in different thread

I want to call a stateless Session Bean Deployed in JBoss 7 from java desktop application.
I create InitialContext instance and everything is ok if I lookup and call bean methods in the same thread (bean1 in following code).
But if I get bean proxy from InitialContext in another thread, the program raises an Exception when I call a method from bean (bean2 in following Code).
Partial source code I use:

public class Tester1 implements Runnable
{
    InitialContext ctx;
    static String beanAddr = "MobileSubscribersService_war_exploded/SubscriberEJB!com.persianswitch.ussd.ISubscriberBeanRemote";

    public void doIt()  throws NamingException
    {
        Properties p = new Properties();
        p.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory");
        p.put(Context.PROVIDER_URL, "remote://localhost:4447");
        p.put(Context.SECURITY_PRINCIPAL, "user");
        p.put(Context.SECURITY_CREDENTIALS, "pass");
        p.put("jboss.naming.client.ejb.context", true);
        ctx = new InitialContext(p);

        ISubscriberBeanRemote bean1 =  (ISubscriberBeanRemote)ctx.lookup(beanAddr);
        int res = bean1.getLanguageByMobileNo("12345"); // this line runs good

        Thread th = new Thread(this);
        th.start();
    }

    @Override
    public void run() {
        try {
            ISubscriberBeanRemote bean2 = (ISubscriberBeanRemote)ctx.lookup(beanAddr);
            int res = bean2.getLanguageByMobileNo("12345"); // Throws Exception Here
        } catch (NamingException e) {
        }
    }
}

Exception content:

Exception in thread "Thread-2" java.lang.IllegalStateException: No EJB receiver available for handling [appName:,modulename:MobileSubscribersService_war_exploded,distinctname:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@3337cdec
    at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:584)
    at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:119)
    at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181)
    at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:136)
    at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:121)
    at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:104)
    at com.sun.proxy.$Proxy0.getLanguageByMobileNo(Unknown Source)
    at com.persianswitch.Tester1.run(Tester1.java:52)
    at java.lang.Thread.run(Thread.java:722)

I want to reuse one InitialContext in multiple threads for improving performance.
Is there any mistake in my code? Are there any rule that I must know?

Upvotes: 2

Views: 1546

Answers (1)

Csaba
Csaba

Reputation: 949

InitialContext is not thread safe as its JavaDoc states: "An InitialContext instance is not synchronized against concurrent access by multiple threads. Multiple threads each manipulating a different InitialContext instance need not synchronize. Threads that need to access a single InitialContext instance concurrently should synchronize amongst themselves and provide the necessary locking."

Upvotes: 1

Related Questions