ejb3相对于ejb2开发方便了很多,所有的东西都可以用注解来完成。不用部署描述文件。。。
jboss6/client下所有的jar包,都要放入classpath,把ejb服务端的接口打成jar包放入client,或者把接口java文件copy过来
怎么查看ejb容器的jndi呢?
在jboss启动时,查看控制台
4:05:06,985 INFO [JBossASKernel] and supplies:
4:05:06,985 INFO [JBossASKernel] jndi:bean12/remote-com.ly.bean.Bean1
4:05:06,985 INFO [JBossASKernel] Class:com.ly.bean.Bean1
4:05:06,985 INFO [JBossASKernel] Class:com.ly.bean.Bean2
4:05:07,001 INFO [JBossASKernel] jndi:bean12/remote-com.ly.bean.Bean2
4:05:07,001 INFO [JBossASKernel] jndi:bean12/remote
4:05:07,001 INFO [JBossASKernel] jndi:bean12
ejb服务端主要架构如下
─src
├─com
│ └─ly
│ ├─bean
│ │ Bean1.java
│ │ Bean2.java
│ │ Bean3.java
│ │
│ └─impl
│ Bean1Impl.java
│ Bean2Impl.java
│
└─META-INF
MANIFEST.MF
package com.ly.bean;
import javax.ejb.Remote;
@Remote
public interface Bean1 {
public void sayBean1();
}
package com.ly.bean;
import javax.ejb.Remote;
@Remote
public interface Bean2 {
public void sayBean2();
}
package com.ly.bean;
import javax.ejb.Remote;
@Remote
public interface Bean3 {
public void sayBean3();
}
package com.ly.impl;
import javax.ejb.Stateless;
import com.ly.bean.Bean1;
import com.ly.bean.Bean2;
@Stateless(name="bean12")
public class Bean1Impl implements Bean1,Bean2 {
public void sayBean1() {
System.out.println("bean1.........");
}
public void sayBean2() {
System.out.println("bean2.........");
}
}
package com.ly.impl;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import com.ly.bean.Bean1;
import com.ly.bean.Bean2;
import com.ly.bean.Bean3;
@Stateless(name="bean3")
public class Bean2Impl implements Bean3 {
@EJB(beanName="bean12")
Bean1 bean1;
Bean2 bean2;
public void sayBean3() {
bean1.sayBean1();
bean2.sayBean2();
System.out.println("bean3.........");
}
@EJB(mappedName="bean12/remote")
public void setBean2(Bean2 bean2) {
this.bean2 = bean2;
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
客户端代码如下:
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.ly.bean.Bean3;
/**
* jboss黑屏客户端输出如下
* <br />
* 14:05:09,481 INFO [org.jboss.bootstrap.impl.base.server.AbstractServer
[6.0.0.Final "Neo"] Started in 34s:101ms<br />
14:05:37,717 INFO [STDOUT] bean1.........<br />
14:05:37,733 INFO [STDOUT] bean2.........<br />
14:05:37,733 INFO [STDOUT] bean3.........<br />
* <br />
* @author Administrator
*
*/
public class Client {
public static void main(String args[]) throws Exception{
Properties pro = new Properties();
pro.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
pro.setProperty("java.naming.provider.url","localhost:1099");
pro.setProperty("java.naming.factory.url.pkgs","org.jboss.naming");
Context context =new InitialContext(pro);
Bean3 bean=(Bean3) context.lookup("bean3/remote");
bean.sayBean3();//远程服务器执行sayBean3方法
}
}
在jboss启动控制台输出如下
n ajp-127.0.0.1-8009
14:05:09,481 INFO [org.jboss.bootstrap.impl.base.server.AbstractServer] JBossAS
[6.0.0.Final "Neo"] Started in 34s:101ms
14:05:37,717 INFO [STDOUT] bean1.........
14:05:37,733 INFO [STDOUT] bean2.........
14:05:37,733 INFO [STDOUT] bean3.........