我在两个不同的Jboss EAP 7实例上运行了两个maven Java应用程序,让我们将它们命名为Client-Application,Server-Application。
我想在Client-Application上进行远程注入,从Server-Application注入一个bean。
我的结构如下:
客户机的应用
我添加了以下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();
}
服务器应用中 本节说明了我的服务器应用程序的外观:
@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
答案 0 :(得分:2)
假设您的客户端 - 应用程序也在您的JBoss EAP 7实例上部署,那么解决方案远比您想象的要简单。
我设置了一个创建四个工件的测试项目:
ServiceBeanInterface
ServiceBean
serverejb
和serverapi
serverapi
在其WEB-INF / lib目录中测试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上找到演示示例。
答案 1 :(得分:1)
在EJB invocations from a remote server instance的JBoss文档站点上有大量文档用于执行实例到实例的远程EJB调用。
配置可能看起来有点复杂,但这主要是出于安全考虑。代码方面仍然相对简单。
如果您仍有问题,我建议您参考此问题,然后提出新问题。