简单的jboss例子
简单的例子
1 创建一个接口IHello
public interface IHello {
public String sayHello(String str);
}
2 创建一个类HelloImpl 实现IHello接口
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless
@Remote({IHello.class})
public class HelloImpl implements IHello{
public String sayHello(String str) {
return str+"--------";
}
}
如果@报错那是需要导包,把jboss的包导过来
导包的步骤是:
1)把jboss的lib.zip解压缩放到任意一个位置,可以是放在工作空间下。
2)右击工程->propertise->Java Build Path->Add Library->User Libary->next->User Libraries,如果已存在就选中,点ok就可以了。
如果没有就按照下面的步骤->new->自己写一个名字->ok就可以了
3 创建一个build.xml文件
<?xml version="1.0"?>
<!-- ======================================================================= -->
<!-- EJB3 HelloWorld build file -->
<!-- ======================================================================= -->
<project name="ejb3one" default="ejbjar" basedir="..">
<property environment="env" />
<property name="app.dir" value="${basedir}/ejb3one" />
<property name="src.dir" value="${app.dir}/src" />
<property name="jboss.home" value="${env.JBOSS_HOME}" />
<property name="jboss.server.config" value="default" />
<property name="build.classes.dir" value="${app.dir}/bin" />
<!-- =================================================================== -->
<!-- Compiles the source code -->
<!-- =================================================================== -->
<target name="ejbjar" description="创建EJB发布包">
<jar jarfile="${app.dir}/ejb3one.jar">
<fileset dir="${build.classes.dir}">
<include name="**/*.class" />
<exclude name="junit/debug/*.*"/>
</fileset>
</jar>
</target>
<target name="deploy" depends="ejbjar">
<copy file="${app.dir}/ejb3one.jar" todir="${jboss.home}/server/${jboss.server.config}/deploy" />
</target>
<!-- =================================================================== -->
<!-- Cleans up generated stuff -->
<!-- =================================================================== -->
<target name="clean">
<delete dir="${build.dir}" />
<delete file="${jboss.home}/server/${jboss.server.config}/deploy/ejb3one.jar" />
</target>
</project>
4 运行build.xml文件。
运行方法是:右击run as -> Ant build
如果发布成功就会在工作空间目录下有ejb3one.jar压缩文件。
5 访问JMX/">Http://localhost:8080->JMX Console->jboss下的service=JNDIView->List of MBean operations:java.lang.String list()下的invoke->Global JNDI Namespace
下的树中如果你创建的实现类在树中也就是看到
+- HelloImpl (class: org.jnp.interfaces.NamingContext)
| +- remote (proxy: $Proxy65 implements interface com.qs.ejb3.IHello,interface org.jboss.ejb3.JBossProxy,interface javax.ejb.EJBObject)
就表示成功
6 创建Client
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Client {
//JNDI:Java Naming and Directory Interface 把名字和一个java对象绑定
//后续程序如果要使用该对象 可以通过jndi的api方法通过名字查找对象
public static void main(String[] args) throws NamingException {
Properties props = new Properties();
//jndi的初始信息
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
//设置访问的url
props.setProperty("java.naming.provider.url", "localhost:1099");
//设置用到的一些包
props.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
Context context = new InitialContext(props);
IHello hello = (IHello)context.lookup("HelloImpl/remote");
String str = hello.sayHello("xiaoxiao");
System.out.println(str);
}
}
运行就可以了