问题
I am trying to access an ejb that is exposed in jbossAS7.1 from code running in an embedded jetty server:
final Properties jndiProperties = new Properties();
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProperties.put(Context.PROVIDER_URL, "remote://127.0.0.1:4447");
jndiProperties.put("jboss.naming.client.ejb.context", true);
jndiProperties.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT", "false");
jndiProperties.put(Context.SECURITY_PRINCIPAL, "ubiant");
jndiProperties.put(Context.SECURITY_CREDENTIALS, "ubiant2iA");
ctx = new InitialContext(jndiProperties);
Object b = ctx.lookup("ejb:Hemis/HemisCoreClient//" + "HardwareDataManagement" + "!" + "com.ubiant.hemis.client.ejb.interf.I_HardwareDataManagement");
But I am getting this exception: "EJB client context selector may not be changed"
Could someone help me with this ?
Here the full stacktrace:
11:17:43,003 ERROR [stderr] (qtp10117087-119) javax.naming.NamingException: Failed to create remoting connection [Root exception is java.lang.RuntimeException: Failed to setup EJB remote context]
11:17:43,011 ERROR [stderr] (qtp10117087-119) at org.jboss.naming.remote.client.ClientUtil.namingException(ClientUtil.java:36)
11:17:43,011 ERROR [stderr] (qtp10117087-119) at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:121)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at javax.naming.InitialContext.init(InitialContext.java:242)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at javax.naming.InitialContext.(InitialContext.java:216)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at authentification.TokenAuthentificationBroker.addConnection(TokenAuthentificationBroker.java:86)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.apache.activemq.broker.MutableBrokerFilter.addConnection(MutableBrokerFilter.java:97)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.apache.activemq.broker.TransportConnection.processAddConnection(TransportConnection.java:733)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.apache.activemq.broker.jmx.ManagedTransportConnection.processAddConnection(ManagedTransportConnection.java:79)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.apache.activemq.command.ConnectionInfo.visit(ConnectionInfo.java:139)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.apache.activemq.broker.TransportConnection.service(TransportConnection.java:292)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.apache.activemq.broker.TransportConnection$1.onCommand(TransportConnection.java:149)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.apache.activemq.transport.TransportSupport.doConsume(TransportSupport.java:83)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.apache.activemq.transport.ws.MQTTSocket.sendToActiveMQ(MQTTSocket.java:112)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.apache.activemq.transport.mqtt.MQTTProtocolConverter.sendToActiveMQ(MQTTProtocolConverter.java:133)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.apache.activemq.transport.mqtt.MQTTProtocolConverter.onMQTTConnect(MQTTProtocolConverter.java:235)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.apache.activemq.transport.mqtt.MQTTProtocolConverter.onMQTTCommand(MQTTProtocolConverter.java:157)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.apache.activemq.transport.ws.MQTTSocket.onMessage(MQTTSocket.java:59)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.eclipse.jetty.websocket.WebSocketConnectionRFC6455$WSFrameHandler.onFrame(WebSocketConnectionRFC6455.java:860)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.eclipse.jetty.websocket.WebSocketParserRFC6455.parseNext(WebSocketParserRFC6455.java:349)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.eclipse.jetty.websocket.WebSocketConnectionRFC6455.handle(WebSocketConnectionRFC6455.java:225)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at java.lang.Thread.run(Thread.java:745)
11:17:43,012 ERROR [stderr] (qtp10117087-119) Caused by: java.lang.RuntimeException: Failed to setup EJB remote context
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.jboss.naming.remote.client.InitialContextFactory.setupEjbContext(InitialContextFactory.java:438)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:115)
11:17:43,012 ERROR [stderr] (qtp10117087-119) ... 25 more
11:17:43,012 ERROR [stderr] (qtp10117087-119) Caused by: java.lang.reflect.InvocationTargetException
11:17:43,012 ERROR [stderr] (qtp10117087-119) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
11:17:43,012 ERROR [stderr] (qtp10117087-119) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
11:17:43,028 ERROR [stderr] (qtp10117087-119) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
11:17:43,028 ERROR [stderr] (qtp10117087-119) at java.lang.reflect.Method.invoke(Method.java:606)
11:17:43,028 ERROR [stderr] (qtp10117087-119) at org.jboss.naming.remote.client.InitialContextFactory.setupEjbContext(InitialContextFactory.java:420)
11:17:43,028 ERROR [stderr] (qtp10117087-119) ... 26 more
11:17:43,028 ERROR [stderr] (qtp10117087-119) Caused by: java.lang.SecurityException: EJB client context selector may not be changed
11:17:43,028 ERROR [stderr] (qtp10117087-119) at org.jboss.ejb.client.EJBClientContext.setSelector(EJBClientContext.java:181)
11:17:43,028 ERROR [stderr] (qtp10117087-119) at org.jboss.naming.remote.client.ejb.RemoteNamingEjbClientContextSelector.setupSelector(RemoteNamingEjbClientContextSelector.java:18)
11:17:43,028 ERROR [stderr] (qtp10117087-119) ... 31 more
回答1:
According to https://developer.jboss.org/thread/273859, "Setting jboss.naming.client.ejb.context = true tries to create an EJB client context selector but it is already created for the JBoss EAP 6 instance," so "It is not possible to change the ejbSelector if the client run inside the [JBoss] server", as you're doing. The recommendation in that thread is to use the scoped-context feature (7.5.4. Using Scoped EJB Client Contexts) for a situation like yours.
BTW, I was thinking that the other approach that might work is to go back to doing a local (non-remote) lookup that relies on the fact that your code is ultimately running inside of JBoss, as you were originally trying to do in your first question (Get EJB object inside rar deployed inside jboss). But https://developer.jboss.org/thread/273428 say "You need to implement the Remote interface to be able to look it up from a different application ear/jar," so I think that's not a viable option, and the way you're approaching it (doing a remote invocation) sounds like the right approach.
EDIT 1, based on the comment on 6/23/2017:
Based on the "Pre-requisites of remotely accessible JNDI objects" section of https://docs.jboss.org/author/display/AS72/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+or+remote-naming+project, EJBs are only remotely exposed when they satisfy two conditions:
1) Such objects should be bound under the java:jboss/exported/ namespace. For example, java:jboss/exported/foo/bar
2) Objects bound to the java:jboss/exported/ namespace are expected to be serializable. This allows the objects to be sent over the wire to the remote clients
When I look at the logs showing EJBs being registered from your original question, I don't see anything to indicate that the EJB you're trying to access is being bound under the java:jboss/exported/
namespace. For the remote lookup to work, I think you'll also need to figure out how to bind your EJB under that namespace, in addition to using a scoped context.
来源:https://stackoverflow.com/questions/44653347/ejb-client-context-selector-may-not-be-changed