jboss7.1.1部署ejb3,调用测总是错误,大牛们有没有解决方案
环境
jboss jboss7.1.1
所使用的包 有 jboss/bin/client/jboss-client.jar
还有从网上找的jnpserver.jar
代码不部分很简单
HelloWorld 接口
public interface HelloWorld { public String SayHello(String name); }
import com.jav.impl.HelloWorld; import javax.ejb.Remote; import javax.ejb.Stateless; @Stateless @Remote(HelloWorld.class) public class HelloWorldBean implements HelloWorld { @Override public String SayHello(String name) { // TODO Auto-generated method stub return name+"Welcome Test"; } }
Properties props=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:org.jnp.interfaces"); try { InitialContext context=new InitialContext(props); HelloWorld hello=(HelloWorld)context.lookup("HelloWorldBean/remote"); System.out.println(hello.SayHello("Hello Kitty")); } catch(NamingException e) { e.printStackTrace(); System.out.println(e.getMessage()); }
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:272)
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1423)
... 4 more
Caused by: javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused: connect]
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:246)
... 5 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)Could not obtain connection to any of these urls: localhost:1099 and discovery failed with error: javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory.java:84)
at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory.java:77)
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:242)
... 5 more
各位大牛 帮帮忙,谁能给个准确的解释
[解决办法]
首先是访问端口是4447,不是以前的1099
访问Bean的方式:
For stateless beans:
ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>
For stateful beans:
ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>?stateful
具体请参考
https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
main:
public static void main(String[] args) { Properties props=new Properties(); props.setProperty(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming"); try { Context context=new InitialContext(props); HelloWorld hello=(HelloWorld)context.lookup("ejb:/EJB1//HelloWorldStatelessBean!helloworld.HelloWorld"); System.out.println(hello.sayHello("dara")); } catch(NamingException e) { e.printStackTrace(); } }
[解决办法]
//HelloWorld 接口:
package com.xrd.ejb3;
public interface HelloWorld {
public String sayHello(String name);
}
//HelloWorldBean 实现类
package com.xrd.ejb3.impl;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import com.xrd.ejb3.HelloWorld;
@Stateless
@Remote(HelloWorld.class)
public class HelloWorldBean implements HelloWorld {
@Override
public String sayHello(String name) {
return name+"说:你好,EJB!";
}
}
//EJBClient 测试类
package com.xrd.test;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.xrd.ejb3.HelloWorld;
public class EJBClient {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
try{
InitialContext ctx = new InitialContext(props);
HelloWorld hw = (HelloWorld) ctx.lookup("ejb:/helloworld//HelloWorldBean!com.xrd.ejb3.HelloWorld");
System.out.println(hw.sayHello("测试EJB3.0"));
}catch(NamingException e){
System.out.println(e.getMessage());
}
}
}
//jboss-ejb-client.properties 文件
endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port=4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=admin
remote.connection.default.password=1234
最后这两句的值admin ,1234 是自己的用户名和密码
运行bin目录下的add-user.bat 增加即可
注意这句:ejb:/ 后面的helloworld 是打包后的名称,com.xrd.ejb3.HelloWorld 一定要写全包路径
HelloWorld hw = (HelloWorld) ctx.lookup("ejb:/helloworld//HelloWorldBean!com.xrd.ejb3.HelloWorld");
测试结果:
2012-5-19 1:55:58 org.jboss.ejb.client.EJBClient <clinit>
INFO: JBoss EJB Client version 1.0.5.Final
2012-5-19 1:55:58 org.xnio.Xnio <clinit>
INFO: XNIO Version 3.0.3.GA
2012-5-19 1:55:58 org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.0.3.GA
2012-5-19 1:55:58 org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 3.2.3.GA
2012-5-19 1:55:59 org.jboss.ejb.client.remoting.VersionReceiver handleMessage
INFO: Received server version 1 and marshalling strategies [river]
2012-5-19 1:55:59 org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
INFO: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@105738, receiver=Remoting connection EJB receiver [connection=Remoting connection <18088c0>,channel=jboss.ejb,nodename=hmily]} on channel Channel ID 85653753 (outbound) of Remoting connection 00f11404 to localhost/127.0.0.1:4447
2012-5-19 1:55:59 org.jboss.ejb.client.remoting.ChannelAssociation$ResponseReceiver handleMessage
WARN: Unsupported message received with header 0xffffffff
测试EJB3.0说:你好,EJB!
JBoss7.11 测试,另外很多人反应JBoss起几分钟后会断掉,不抱错,是因为JBOss没有加用户