具有本地接口的无状态bean
如果客端端与服务端发运行在同一机器的同一jboss中时,可以直接调用本地接口,通过内存直接交互
示例代码如下:
/**
* Copyright (C) 2015
*
*FileName:HelloWorld.java
*
* Author:<ahref="mailto:zhenhuayue@sina.com">Retacn</a>
*
* CreateTime:2015-1-14
*/
// Package Information
package cn.yue.ejb.bean;
/**
* ejb客户端通过使用这个接口引用从EJB容器中得到会话bean对象stub
*
* @version
* @Description:
* @author <ahref="mailto:zhenhuayue@sina.com">Retacn</a>
* @since 2015-1-14
*/
public interface HelloWorld {
public StringSayHello(String name);
}
/**
* Copyright (C) 2016
*
*FileName:LocalHelloWorld.java
*
* Author:<ahref="mailto:zhenhuayue@sina.com">Retacn</a>
*
* CreateTime:2016-7-20
*/
// Package Information
package cn.yue.ejb.bean;
/**
* 本地访问接口
*
* @version
* @Description:
* @author <ahref="mailto:zhenhuayue@sina.com">Retacn</a>
* @since 2016-7-20
*/
public interface LocalHelloWorld extends HelloWorld {
public StringlocalSay(String name);
}
/**
* Copyright (C) 2016
*
*FileName:RemoteHelloWorld.java
*
* Author:<ahref="mailto:zhenhuayue@sina.com">Retacn</a>
*
* CreateTime:2016-7-20
*/
// Package Information
package cn.yue.ejb.bean;
/**
* 远程访问接口
*
* @version
* @Description:
* @author <ahref="mailto:zhenhuayue@sina.com">Retacn</a>
* @since 2016-7-20
*/
public interface RemoteHelloWorld extends HelloWorld {
}
/**
* Copyright (C) 2015
*
*FileName:HelloWorldBean.java
*
* Author:<ahref="mailto:zhenhuayue@sina.com">Retacn</a>
*
* CreateTime:2015-1-14
*/
// Package Information
package cn.yue.ejb.bean;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
/**
* 实现业务接口 并添加两个注释
*
* @version
* @Description:
* @author <ahref="mailto:zhenhuayue@sina.com">Retacn</a>
* @since 2015-1-14
*/
// 指定为无状态会话bean
@Stateless
// 指定远程接口
@Remote(RemoteHelloWorld.class)
// 指定为本地接口
@Local(LocalHelloWorld.class)
public class HelloWorldBean implements LocalHelloWorld,RemoteHelloWorld {
@Override
public StringSayHello(String name) {
// try {
// Propertiesprops = new Properties();
//props.setProperty("java.naming.factory.initial",
//"org.jnp.interfaces.NamingContextFactory");
//props.setProperty("java.naming,provider.url","localhost:1099");
//props.setProperty("java.naming.factory.url.pkgs",
//"org.jboss.naming");
//
//InitialContext ctx;
// ctx = newInitialContext(props);
// HelloWorldhelloworld = (HelloWorld)
//ctx.lookup("HelloWorldBean/romote");
// //out.println(helloworld.SayHello("retacn"));
// } catch(NamingException e) {
//e.printStackTrace();
// }
return name +" say:hello world! this is my first EJB test!";
}
@Override
public StringlocalSay(String name) {
return"local---" + name + " say:hello world! this is my first EJBtest!";
}
}
客记端本地访问程序目录结构如下:
HelloWorld.jar为上面代码src打包
index.jsp示例代码如下
<%@pageimport="cn.yue.ejb.bean.LocalHelloWorld"%>
<%@pageimport="javax.naming.Context"%>
<%@pageimport="java.util.Hashtable"%>
<%@pageimport="cn.yue.ejb.bean.HelloWorld"%>
<%@pageimport="javax.naming.NamingException"%>
<%@pageimport="javax.naming.InitialContext"%>
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>Hello EJB</title>
</head>
<body>
<h1>Hello EJB!</h1>
<%--通过本接口访问 --%>
<%
finalHashtable jndiProperties = newHashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
try {
final String appName = "";
final String moduleName = "HelloWorldEJB";
final String distinctName = "";
final String beanName = "HelloWorldBean";
final String viewClassName = LocalHelloWorld.class.getName();
final String namespace = "ejb:" + appName + "/" + moduleName + "/" + distinctName
+ "/"
+ beanName + "!" + viewClassName;
out.println("namespace:" + namespace);
InitialContext ctx = new InitialContext();
LocalHelloWorld helloworld =(LocalHelloWorld) ctx.lookup(namespace);
out.println(helloworld.localSay("retacn"));
} catch (NamingException e) {
out.println(":" + e.getStackTrace().toString());
out.println(":" + e.getMessage());
out.println(":" + e.getExplanation());
}
%>
</body>
</html>