1.通过这个例子可以区分,有状态bean和无状态bean中的数据会不会混淆;
2.一种取巧的做法是:在建立的客户端的工程上右键,选择build path--->把服
务端的EJB工程导入的,新建立的java客户端里面
3.正规的工作时候,还是需要在相应的接口类中打包,然后把打包好的.jar文件
放到,新建的java project客户端工程里的目录下的;
4.异常:
Need to specify class name in environment or system property, or
as an applet parameter, or in an application resource file:
java.naming.factory.initial
原因:
1.没有写入jndi.properties.
2.Need to specify class name in environment or system property. 的
意思就是找不到你的EJB环境变量. 换句话说就是你的客户在使用lookup
的时候. context没有设置对.
3.Need to specify class name in environment or system property, or
as an applet parameter, or in an application resource file:
java.naming.factory.initial
网上搜了半天,都是说配置不对,但我使用的是代码InitialContext
ctx=new InitialContext(props);的方式,不存在jndi配置文件不对的问
题,最后想起之前一个教程里的一句命名空间的话,于是登录jboss控制
台,找到部署的ejb,前面一个java提醒鸟我,将代码里lookup中参数前
面加上java:,搞定。
public class EjbClient {
public static void main(String[] args) {
Properties props=new Properties();
props.setProperty
("java.naming.factroy.initial","org.jnp.interfaces.NamingContextFact
ory");
props.setProperty
("java.naming.provider.url","localhost:1099");//或者
jnp://localhost...也可
props.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");//此行必须有
try{
InitialContext ctx=new InitialContext(props);
HelloWorld helloworld=(HelloWorld)ctx.lookup
("java:HelloWorldBean/remote");//前面的java是关键
System.out.println(helloworld.sayHello("jack"));
}catch(NamingException e){
System.out.println("error:"+e.getMessage());
}
}
}
-----------------------------------------------------------------
5.注意:如果不想用jndi.properties也可以这样:
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
p.put(Context.PROVIDER_URL, "tcp://localhost:61616");
p.put(Context.SECURITY_PRINCIPAL, "system");
p.put(Context.SECURITY_CREDENTIALS, "manager");
p.put("connectionFactoryNames", "topicCF");
p.put("topic.topic1", "jms.topic1");
InitialContext ctx=new InitialContext(p);
-----------------------------------------------------------
1.Exception in thread "main" javax.naming.NameNotFoundException:
StatelessEjbBean not bound
a.由于没有加入EJB的jar包;
b.EJB项目没有部署
------------------------------------------------------------
EJB3.0框架实例:
项目作用:通过这个例子可以区分,有状态bean和无状态bean中的数据会不会混
淆;
1.建立服务端EJB3.0项目:EJBTest_02
在:com.credream.ejb下:
StatefulEjb.java
package com.credream.ejb;
public interface StatefulEjb {
public void compute(int i);
public int getResult();
}
------------------------------------------
StatefulEjbBean.java
package com.credream.ejb;
import javax.ejb.Remote;
import javax.ejb.Stateful;
@Stateful
@Remote
public class StatefulEjbBean implements StatefulEjb {
private int state;
public void compute(int i) {
state=state+i;
}
public int getResult() {
return state;
}
}
------------------------------------------------------------
StatelessEjb.java
package com.credream.ejb;
public interface StatelessEjb {
public void compute(int i);
public int getResult();
}
-------------------------------------------
StatelessEjbBean.java
package com.credream.ejb;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless
@Remote
public class StatelessEjbBean implements StatelessEjb {
private int state;
public void compute(int i) {
state=state+i;
}
public int getResult() {
return state;
}
}
-------------------------------------------------------------------
2.建立客户端JAVAproject:EJBTest02_client
建立jndi.properties文件
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost
3.导入EJB的jar包;
4.com.credream.ejb下coding
StatefulEjbClient.java
package com.credream.ejb;
import javax.naming.InitialContext;
public class StatefulEjbClient {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
InitialContext context = new InitialContext();
//第一次会话
StatefulEjb ejb1=(StatefulEjb)context.lookup
("StatefulEjbBean/remote");
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
//第二次会话
StatefulEjb ejb2=(StatefulEjb) context.lookup
("StatefulEjbBean/remote");
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
}
}
-------------------------------------------------------------------
StatelessEjbClient.java
package com.credream.ejb;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class StatelessEjbClient {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
InitialContext context = new InitialContext();
//第一次会话
StatelessEjb ejb1=(StatelessEjb)context.lookup
("StatelessEjbBean/remote");
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
//第二次会话
StatelessEjb ejb2=(StatelessEjb) context.lookup
("StatelessEjbBean/remote");
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
//虽然是去的同一个服务端的对象,但是每次的客户端调用 的服务端的对
象都是不同的
System.out.println("ejb1==ejb2"+(ejb1==ejb2));
}
}
--------------------------------------------------------------------