客户端Jboss 7多个线程

时间:2015-09-28 18:44:03

标签: java multithreading swing jboss jboss7.x

我有一个连接到Jboss 7 AS的swing应用程序。 调用一些后台线程会导致客户端没有这样的ejb错误。 这是一个例子

package com.asf.capone.client.util;

import java.util.Hashtable;

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

import com.asf.capone.common.exception.AppException;

import ro.asf.capone.ejb.beans.security.SecurityController;
import ro.asf.capone.ejb.beans.security.SecurityControllerRemote;

public class TestJndi {
public static void main(final String[] args) throws AppException {
    final Hashtable env = new Hashtable();
    env.put("java.naming.factory.initial", "org.jboss.naming.remote.client.InitialContextFactory");
    env.put("java.naming.provider.url", "remote://localhost:4447");
    env.put("java.naming.security.credentials", "c4ca4238a0b923820dcc509a6f75849b");
    env.put("java.naming.security.principal", "capone");
    env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    env.put("jboss.naming.client.ejb.context", "true");
    env.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT", "false");

    try {
        final InitialContext ctx = new InitialContext(env);
        System.out.println("ctx: " + ctx);
        final SecurityController o = (SecurityControllerRemote) ctx.lookup(
                "ejb:agency-ear/agency-ejb/SecurityControllerBean!ro.asf.capone.ejb.beans.security.SecurityControllerRemote");
        System.out.println("1outcome: " + o.getServerTimeMillis());

        new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println("2outcome: " + o.getServerTimeMillis());
            }
        }).start();

    } catch (final NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

这个输出是:

ctx: javax.naming.InitialContext@307f6b8c
1outcome: 1443465336127
Exception in thread "Thread-4" java.lang.IllegalStateException: EJBCLIENT000025: No EJB receiver available for handling [appName:agency-ear, moduleName:agency-ejb, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@381dfddb
at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:754)
at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:116)
at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:186)
at org.jboss.ejb.client.EJBInvocationHandler.sendRequestWithPossibleRetries(EJBInvocationHandler.java:253)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:198)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:181)
at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:144)
at com.sun.proxy.$Proxy2.getServerTimeMillis(Unknown Source)
at com.asf.capone.client.util.TestJndi$1.run(TestJndi.java:36)
at java.lang.Thread.run(Thread.java:745)

我遗漏了一些应该允许我在两个电话上获得相同输出的东西,但我无法确定问题是什么。谢谢!

1 个答案:

答案 0 :(得分:0)

看起来这不适用于那个版本的Jboss(他们已经改变了遥控器),因为我的初始代码在Jboss 7.3.0中有效。我目前的Jboss版本是JBoss EAP 6.4.0.GA(AS 7.5.0.Final-redhat-21) 现在可以使用的代码是:

import java.util.Properties;

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

import org.jboss.ejb.client.ContextSelector;
import org.jboss.ejb.client.EJBClientConfiguration;
import org.jboss.ejb.client.EJBClientContext;
import org.jboss.ejb.client.PropertiesBasedEJBClientConfiguration;
import org.jboss.ejb.client.remoting.ConfigBasedEJBClientContextSelector;

import ro.asf.capone.ejb.beans.security.SecurityControllerRemote;


public class AppJboss {
    public static void main(String[] args) throws NamingException {
       System.out.println("Hello World!");
       final String lookup = "ejb:agency-ear/agency-ejb//SecurityControllerBean!ro.asf.capone.ejb.beans.security.SecurityControllerRemote";

       final Properties clientProperties = new Properties();
       clientProperties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS",
            "JBOSS-LOCAL-USER");
       clientProperties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT",
            "false");
       clientProperties.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
       clientProperties.put("remote.connections", "default");
       clientProperties.put("endpoint.name", "client-endpoint");
       clientProperties.put("remote.connection.default.port", "4447");
       clientProperties.put("remote.connection.default.host", "127.0.0.1");
       clientProperties.put("remote.connection.default.username", "capone");
       clientProperties.put("remote.connection.default.password", "c4ca4238a0b923820dcc509a6f75849b");
       clientProperties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS",
            "false");

       final EJBClientConfiguration ejbClientConfiguration = new PropertiesBasedEJBClientConfiguration(
            clientProperties);
       final ContextSelector<EJBClientContext> contextSelector = new ConfigBasedEJBClientContextSelector(
            ejbClientConfiguration);
       EJBClientContext.setSelector(contextSelector);

       final Properties properties = new Properties();
       properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
       final Context context = new InitialContext(properties);
       final SecurityControllerRemote myBean = (SecurityControllerRemote) context.lookup(lookup);
       final long result = myBean.getServerTimeMillis();
       System.out.println("result " + result);

       new Thread(new Runnable() {

          public void run() {
              final long result = myBean.getServerTimeMillis();
              System.out.println(result);
          }
       }).start();
    }
}

客户端库取自jboss / bin / client / jboss-client.jar

同样的代码也适用于Wildfly,只需更改端口和客户端库。希望这有助于其他人。