jboss7.1.1部署ejb3,调用测总是环境
jboss jboss7.1.1
所使用的包 有 jboss/bin/client/jboss-client.jar
还有从网上找的jnpserver.jar
代码不部分很简单
HelloWorld 接口
Java code
public interface HelloWorld {
public String SayHello(String name);
}
HelloWorldBean
Java code
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";
}
}
Test代码
Java code
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());
}
部署上面的HelloWorld 和HelloWorldBean 的jar包之后
java:global/Hello/HelloWorldBean!com.jav.impl.HelloWorld
java:app/Hello/HelloWorldBean!com.jav.impl.HelloWorld
java:module/Hello/HelloWorldBean!com.jav.impl.HelloWorld
java:jboss/exported/Hello/HelloWorldBean!com.jav.impl.HelloWorld
java:global/Hello/HellWorldBean
java:app/Hello/HellWorldBean
java:module/Hello/HellWorldBean
启动之后出现上面的代码
但是我Test类做测试的时候还是报出异常,但是我用jboss-6.1.0.Final 做测试的同样的代码没有错误,问题应该还是在jboss7上
错误时
javax.naming.CommunicationException: 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] [Root exception is javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]]
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1452)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:597)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:590)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.jav.test.Test.main(Test.java:20)
Caused by: javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is 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: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
各位大牛 帮帮忙,谁能给个准确的解释
对我有用[0]丢个板砖[0]引用举报管理TOP回复次数:9
ZuxiangHuang
(zuxianghuang)
等 级:
#1楼 得分:0回复于:2012-04-11 09:28:38ejb类没有问题,
看错误信息是,localhost:1099 这个不能连接。
6没问题,?难道是7改了?
没搞过7,不好确定。
到jboss社区看看
对我有用[0]丢个板砖[0]引用举报管理TOP精华推荐:借人气宣传一下自己开发的语言Dao
cseu
(stay cool)
等 级:
#2楼 得分:0回复于:2012-04-11 13:52:40改为IP:1099试试,绑定IP和localhost是不一样的
对我有用[0]丢个板砖[0]引用举报管理TOP精华推荐:用汇编修改dos版三国英杰传支持键盘操作__附带制作过程
cryingzgz
(坚持走C/C++路线)
等 级:
#3楼 得分:50回复于:2012-04-11 23:07:07首先是访问端口是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:
Java code
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();
}
}
jboss-ejb-client.properties(classpath下):
Java code
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=yourUsername
remote.connection.default.password=yourPassword
需要引入的包:jboss-client.jar
对我有用[0]丢个板砖[0]引用举报管理TOP精华推荐:一个关于mov占用时钟周期的问题。
cryingzgz
(坚持走C/C++路线)
等 级:
#4楼 得分:0回复于:2012-04-11 23:19:28jboss社区的示例:
Java code
package helloworld;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Test {
public static void main(String[] args) throws NamingException {
final Properties jndiProperties = new Properties();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
final String appName = "";
//部署的jar文件的名字
final String moduleName = "EJB1";
final String distinctName = "";
//实现类的类名
final String beanName = HelloWorldStatelessBean.class.getSimpleName();
//接口类的全名
final String viewClassName = HelloWorld.class.getName();
String lookupStr = "ejb:" + appName + "/"
+ moduleName + "/" + distinctName + "/" + beanName + "!"
+ viewClassName;
HelloWorld he = (HelloWorld)context.lookup(lookupStr);
System.out.println(he.sayHello("DARA"));
}
}
jboss-ejb-client.properties 同上
//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没有加用户