FirSesBean代码:
package seslesspro;import javax.ejb.*;public class FirSesBean implements SessionBean {
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public String getName() {
/**@todo Complete this method*/
return "Testing name Successful!";
}
}
package seslesspro;import javax.ejb.*;public class FirSesBean implements SessionBean {
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public String getName() {
/**@todo Complete this method*/
return "Testing name Successful!";
}
}
import javax.rmi.PortableRemoteObject;public class FirSesTestClient1 extends Object {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private FirSesHome firSesHome = null;
private FirSes firSes = null; //Construct the EJB test client
public FirSesTestClient1() {
initialize();
} public void initialize() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
} try {
//get naming context
Context context = getJBossInitialContext(); //look up jndi name
Object ref = context.lookup("FirSes");
//look up jndi name and cast to Home interface
firSesHome = (FirSesHome) PortableRemoteObject.narrow(ref, FirSesHome.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing local bean access through Local Home interface.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed initializing bean access.");
}
e.printStackTrace();
}
}
private javax.naming.Context getJBossInitialContext() throws NamingException {
java.util.Hashtable JNDIParm = new java.util.Hashtable();
JNDIParm.put(Context.PROVIDER_URL, "localhost");
JNDIParm.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
return new InitialContext(JNDIParm);
}
//----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//---------------------------------------------------------------------------- public FirSes create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
firSes = firSesHome.create();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: create()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: create()");
}
e.printStackTrace();
} if (logging) {
log("Return value from create(): " + firSes + ".");
}
return firSes;
} //----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//---------------------------------------------------------------------------- public String getName() {
String returnValue = ""; if (firSes == null) {
System.out.println("Error in getName(): " + ERROR_NULL_REMOTE);
return returnValue;
} long startTime = 0;
if (logging) {
log("Calling getName()");
startTime = System.currentTimeMillis();
} try {
returnValue = firSes.getName();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: getName()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: getName()");
}
e.printStackTrace();
} if (logging) {
log("Return value from getName(): " + returnValue + ".");
}
return returnValue;
} //----------------------------------------------------------------------------
// Utility Methods
//---------------------------------------------------------------------------- private void log(String message) {
if (message == null) {
System.out.println("-- null");
return ;
}
if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");
}
else {
System.out.println("-- " + message);
}
}
//Main method public static void main(String[] args) {
FirSesTestClient1 client = new FirSesTestClient1();
// Use the client object to call one of the Home interface wrappers
// above, to create a Remote interface reference to the bean.
// If the return value is of the Remote interface type, you can use it
// to access the remote interface methods. You can also just use the
// client object to call the Remote interface wrappers. try {
client.create();
String name = client.getName();
System.out.println("Name from the Test Client = " + name);
}
catch (Exception ex) { } }
}
2、你的jndi怎么配置的?
3、你发布后jboss状态正确吗4、建议使用servlet测试
-- Initializing bean access.
javax.naming.CommunicationException: Receive timed out. Root exception is java.net.SocketTimeoutException: Receive timed out
at java.net.PlainDatagramSocketImpl.receive(Native Method)
at java.net.DatagramSocket.receive(DatagramSocket.java:671)
at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:953)-- Failed initializing bean access.
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1040)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:450)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:443)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at seslesspro.FirSesTestClient1.initialize(FirSesTestClient1.java:30)
at seslesspro.FirSesTestClient1.<init>(FirSesTestClient1.java:15)
at seslesspro.FirSesTestClient1.main(FirSesTestClient1.java:143)
-- Calling create()
java.lang.NullPointerException
at seslesspro.FirSesTestClient1.create(FirSesTestClient1.java:65)
at seslesspro.FirSesTestClient1.main(FirSesTestClient1.java:151)
-- Failed: create()
-- Return value from create(): null.
Error in getName(): Remote interface reference is null. It must be created by calling one of the Home interface methods first.
Name from the Test Client =
非常感谢!
jndi需要配置吗?怎么配置?配置哪个文件?或其它!
Initializing bean access.
初始化的时候就出错了,可能是EJB容器的问题吧。容器启动后在,jb8左边的项目列表中找到XXXX.jar,右键部署,成功部署后再run client
一切OK
Object ref = context.lookup("FirSes");
//look up jndi name and cast to Home interface
firSesHome = (FirSesHome) PortableRemoteObject.narrow(ref, FirSesHome.class);用servlet测试,输出
FirSes not bound初步确定是jndi问题,Home JNDI name 设置为:FirSes
内容
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost这些是要根据具体的容器来改的
要在你class路径里
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces这个jndi.properties文件在jboss中client目录下的jbossjmx-ant.jar文件里,通常jboss的缺省端口号是1099,所以必须在jndi.properties里将java.naming.provider.url配置成和你访问的jboss服务匹配的url。如果觉得改jndi.properties不方便的话,建议在你的client程序里的initialize()函数中的 Object ref = context.lookup("FirSes"); 语句之前加上如下代码:
System.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
System.setProperty("java.naming.provider.url", "localhost:1099");
-Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory -Djava.naming.provider.url=localhost:1099 -Djava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
没有设置Application parameters我测试了,按你们的方法,出现的错误一样!
太打击学习积极性了!!!我坚持!
<!DOCTYPE jboss PUBLIC '-//JBoss//DTD JBOSS 3.0//EN' 'http://www.jboss.org/j2ee/dtd/jboss_3_0.dtd'>
<jboss>
<enterprise-beans>
<session>
<ejb-name>FirSes</ejb-name>
<jndi-name>FirSes</jndi-name>
</session>
</enterprise-beans>
</jboss>ejb-jar.xml为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<session>
<display-name>FirSes</display-name>
<ejb-name>FirSes</ejb-name>
<home>seslesspro.FirSesHome</home>
<remote>seslesspro.FirSes</remote>
<ejb-class>seslesspro.FirSesBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>FirSes</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
很显然是你环境的问题...估计你的jndi还是没搞定..如果可以的话,你可以手动在控制台下进行编译,打包,发布,然后运行test..
这样你或许更清楚你哪些包或环境不对.
http://mattkelli.com/tech/jboss/jbuilder7/
我已经看了,thanks为什么看不到1099是激活的?JNDI还能用吗?
我有点不好意思说,是我的jboss没启动。我一直以为直接点击client->右键->run using "client",
jboss就会在JB8里启动运行,其它相关的服务也会启动,现在
发现我错了。还好发现JNDI服务没启动,同时shaomickey(不够刺激)
提醒了我 ,在此非常感谢。
也可以在jbuilder中运行jboss啊,上次给你的网址里说得很详细,自己看一看吧!!!!
http://mattkelli.com/tech/jboss/jbuilder7/老大,我是这个论坛的新手,今天忙了一上午了,头昏眼花,有么有给分啊??!!!
呵呵~~~~~!!!