一个小的调用本地EJB的作业,不难,接口什么的非常好写,但是由于网上的jndi千奇百怪,而且这个东西太老了,我上课也没听,导致只能搞个远程的,本地的概念什么的都不是很清楚,所以弄了很久。
一会有空总结一下,主要参考老师的PPT和这两篇博客。
非常感谢
总结一下:
会话Bean远程调用
2种方法:
1)使用通用JNDI API
2)使用EJB Client API
此处我们PPT上写的很好,直接粘贴了(EJB项目均已建立完成)
1)JNDI
public class StatelessRemoteClient {
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
//服务器的命名和目录管理地址
prop.put(Context.PROVIDER_URL, "remote://localhost:4447");
//初始化上下文环境工厂
prop.put(Context.INITIAL_CONTEXT_FACTORY,
org.jboss.naming.remote.client.InitialContextFactory.class.getName());
//用户验证
prop.put(Context.SECURITY_PRINCIPAL,
System.getProperty("username","testJNDI"));
prop.put(Context.SECURITY_CREDENTIALS,
try {System.getProperty("password","123456"));
Context ctx = new InitialContext(prop);
Object obj = ctx.lookup ("SessionEJB/HelloBean!javaee.ejb.stateless.remote.HelloBeanRemote");
/*实际访问的是java:jboss/exported/SessionEJB/HelloBean!javaee.ejb.stateless.remote.HelloBeanRemote
*/
HelloBeanRemote hwr = (HelloBeanRemote)obj;
String say = hwr.sayHello("Jilin University");
System.out.println(say);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2)EJB API
2)使用EJB Client API
²a) 新建一个普通Java Project工程,把jboss-client.jar加入到项目(右键工程-Build Path –Configure Build Path );
²b) 将HelloBeanRemote.java文件按照原有路径拷贝到本工程中;
²c) 创建客户端测试文件StatelessRemoteClient.java
(带有main函数),如程序清单8-3所示;
²d) 在src目录下添加“jboss-ejb-client.properties”文件。其内容如程序清单8-4所示:程序及propertities文件如下:
public class StatelessRemoteClient {
public static void main(String[] args) {
//在JBoss中使用如下方式访问EJB
Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
try {
Context context = new InitialContext(jndiProperties);
final String appName = "";
final String moduleName = "SessionEJB";
final String distinctName = "";
Object obj = context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/HelloBean!javaee.ejb.stateless.remote.HelloBeanRemote");
(通过接口访问)
HelloBeanRemote hwr = (HelloBeanRemote)obj;
String say = hwr.sayHello("Jilin University");
System.out.println(say);
} catch (NamingException e) {
e.printStackTrace();
}
}
}
endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port=4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=lsq
remote.connection.default.password=123456
本地调用方法:我自己找的+实验:
功能:;输出系统时间
新建一个EJB项目,建立本地接口及实现,右键项目-Export-EJB JAR FILE,到一个目录下
调用:新建一个动态WEB项目,拷贝上面项目的JAR包到WEB-INF的lib目录下,将EJB项目中接口的源文件(完整路径包括包拷贝到src目录下)
2种方法调用的代码如下:
一种为Servlet中调用:
package EJBServletTest;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestServlet
*/
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB javaee.ejb.stateless.TimeBeanLocal tb;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out=null;
try{
response.setContentType("text/html;charset=UTF-8");
out = response.getWriter();
out.println("");
out.println("");
out.println("依赖注入无状态会话Bean本地接口 ");
out.println("");
out.println("");
String str=tb.printTime();
out.print("
通过依赖注入调用本地接口EJB
"+str+"
");
}catch(Exception e){
e.printStackTrace();
out.print("
本地接口调用失败!");
}finally{
out.println("");
out.println("");
out.flush();
out.close();
}
}
}
一种为JSP中调用
<%@ page language="java" import="javax.naming.*, javaee.ejb.stateless.*,java.util.*" pageEncoding="gb2312"%>
JNDI无状态会话Bean本地接口
<%
System.out.println("1111");
try{
InitialContext ctx = new InitialContext();
final String appName = "";
final String moduleName = "homework2_client";
final String distinctName = "";
//实现类名
final String beanName = "TimeBean";
//out.println(beanName);
//接口类名
final String viewClassName = TimeBeanLocal.class.getName();
//out.println(viewClassName);
String jndi = "java:app/"+moduleName+"/" + beanName + "!" + viewClassName;
// String jndi = "java:module/" + beanName + "!" + viewClassName;
//out.println(jndi);
TimeBeanLocal tb = (TimeBeanLocal)ctx.lookup(jndi);
//TimeBeanLocal tb = (TimeBeanLocal)ctx.lookup("java:module/TimeBean!javaee.ejb.stateless.TimeBeanLocal");
String str=tb.printTime();
out.print("
通过JNDI调用本地接口EJB
"+str+"
");
}catch(Exception e){
System.out.println(e.getMessage());
out.print("
本地接口调用失败!");
}
%>
然后将EJB项目先运行,再运行上面的两个网页即可
项目结构如下