我有两个maven Java应用程序运行在两个不同的Jboss 7实例上,让我们将它们命名为Client-Application,。
我想在客户端应用程序上进行远程注入,从注入一个bean。
我的结构如下:
Client-Application
我添加了以下maven依赖项:
<dependency>
<groupId>org.jboss.eap</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<type>pom</type>
</dependency>这个应用程序上的主类和接口如下所示:
public void remoteInjectionTest() {
try {
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
jndiProperties.put("java.naming.factory.initial", "org.jboss.as.naming.InitialContextFactory");
jndiProperties.put("java.naming.provider.url", "localhost");
jndiProperties.put("jboss.naming.client.ejb.context", "true");
Context context = new InitialContext(jndiProperties);
String jndi = "java:global/Server-Application/ServiceBean!test.package.ServiceBeanInterface";
serviceInterface = (ServiceBeanInterface) context.lookup(jndi);
return serviceInterface.getHelloString();
} catch (NamingException e) {
e.printStackTrace();
}
}和接口:
public interface ServiceBeanInterface {
public String getHelloString();
}Server-Application本节展示了我的服务器应用程序的样子:
@Stateless
@Remote(ServiceBeanInterface.class)
public class ServiceBean implements ServiceBeanInterface {
@Override
public String getHelloString() {
return "Hello";
}
}我还试图在服务器应用程序接口上添加一个@Remote,但还是没有成功。
@Remote
public interface ServiceBeanInterface {
public String getHelloString();
}有什么建议吗?
我得到的错误是:
由: java.lang.ClassCastException: com.sun.Proxy引起。$Proxy118不能转换为test.package.ServiceBeanInterface
发布于 2017-03-06 13:28:44
假设您的Client-Application也部署在JBoss EAP 7实例上,那么解决方案比您想象的要简单得多。
我建立了一个测试项目,它创建了四个工件:
ServiceBeanInterfaceServiceBean的serverejb和serverapi的serverapp - EARserverapi测试servlet如下所示:
@WebServlet(urlPatterns = "/")
public class Client extends HttpServlet {
// Inject remote stub
@EJB(lookup="java:global/serverapp-1.0-SNAPSHOT/serverejb-1.0-SNAPSHOT/ServiceBean!com.stackoverflow.p42618757.api.ServiceBeanInterface")
private ServiceBeanInterface serviceBeanInterface;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write(serviceBeanInterface.getHelloString());
}
}serverapp和clientapp被部署到服务器上。
点击http://localhost:8080/clientapp-1.0-SNAPSHOT/就能得到预期的Hello。
不需要老式的JNDI查询。
但是,如果您真的想进行JNDI查找,那么只需要:
Context initialContext = new InitialContext(); // no props needed in the same server instance
ServiceBeanInterface serviceBeanInterface
= (ServiceBeanInterface)initialContext.lookup("java:global/serverapp-1.0-SNAPSHOT/serverejb-1.0-SNAPSHOT/ServiceBean!com.stackoverflow.p42618757.api.ServiceBeanInterface");在GitHub上可以找到一个示例。
发布于 2017-03-07 11:24:39
在JBoss文档站点上,来自远程服务器实例的EJB调用上有大量用于执行实例到实例的远程EJB调用的文档。
配置看起来可能有点复杂,但这主要是出于安全原因。代码端仍然相对简单。
我建议你参考这个,如果你还有问题的话,再问一个新的问题。
https://stackoverflow.com/questions/42618757
复制相似问题