jgitter的回答給了我最大的方式。這是我結束了:
/**
* @return a reference to the EJB
* @throws EjbLookupException
*/
@NotNull
public T lookup()
throws EjbLookupException
{
String path = createJndiPath();
Context initialContext = null;
try
{
initialContext = createInitialContext();
//noinspection unchecked
final T ejb = (T)initialContext.lookup(path);
if(m_apiVersion != null)
{
((RemoteAPI)ejb).validateClientCompatibility(m_apiVersion);
}
return ejb;
}
catch(NamingException | RuntimeException e)
{
throw new EjbLookupException("Unable to find the JBoss EJB at " + path, e);
}
finally
{
if(initialContext != null)
{
//noinspection ThrowableResultOfMethodCallIgnored
Closer.close(initialContext);
}
}
}
/**
* There are a lot of ways to do JBoss 7/Wildfly EJB lookups. Using this method, we don't have to create
* outbound socket bindings whenever we want to use a remote EJB.
*
* @throws NamingException
*/
@NotNull
private Context createInitialContext()
throws NamingException
{
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
properties.put("org.jboss.ejb.client.scoped.context", "true");
properties.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false");
properties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", "false");
properties.put("remote.connections", "default");
properties.put("remote.connection.default.host", m_host);
properties.put("remote.connection.default.port", String.valueOf(m_port));
if(m_username != null)
{
properties.put("remote.connection.default.username", m_username);
}
if(m_password != null)
{
properties.put("remote.connection.default.password", m_password);
}
return new InitialContext(properties);
}
public static class EjbLookupException
extends Exception
{
EjbLookupException (
@NotNull String message,
@NotNull Throwable cause)
{
super(message, cause);
}
}
我不知道如果我需要一個scoped context,我可能無法正確關閉連接。我會根據我的發現更新這個答案。
當你在wildly而不是tomcat中使用相同的代碼時會發生什麼? –
您是否知道您可以爲您的Ops團隊提供一個六行CLI腳本,它將爲您設置遠程出站連接?這有利於將遠程主機配置與應用程序完全分開。只是別的可以考慮... –
在蜻蜓工作的代碼生成的「EJB客戶端上下文選擇器可能不會改變」在蜻蜓 –