jboss5.0的简单测试

此例子的前提是把jboss都配置成功后的一个小的例子(以无状态会话bean为例,在eclipse-SDK-3.5.1-win32实现):

每个会话bean都需要有一个bean接口和一个bean类,其中bean接口是客户端代码和bean内部交互的机制,而bean类是内部方法的实现;一个会话bean的业务逻辑实现是在它的bean类中。会话bean的bean类还必须实现javax.ejb.SessionBean 接口或者用元数据描述符@stateless作为类声明的前缀。

创建工程SimpleSessionApp,然后通过properties=>Java Bulid Path =>Libraries的Add External JARS... 把C:\jboss-5.1.0.GA\client下的jar包全部加入

接口SimpleSession.java

package com.ejb;

public interface SimpleSession {
public String getEchoString(String clientString);
}


接口实现SimpleSessionBean.java

package com.ejb;

import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless
@Remote({SimpleSession.class})
public class SimpleSessionBean implements SimpleSession{
public String getEchoString(String clientString){

return clientString + " - from session bean";
}
}

客户端代码SimpleSessionClient.java:

package com.client;

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ejb.SimpleSession;
public class SimpleSessionClient {

/**
* @param args
*/
public static void main(String[] args) {
Hashtable<String, String> props = new Hashtable<String, String>();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); //JNDI驱动类名,它类似与JDBC指定驱动类
props.put(Context.PROVIDER_URL, "localhost:1099"); //命名服务提供者的的URL,包含提供命名服务的主机地址和端口号,它类似与JDBC指定数据的连接URL
props.put("java.naming.rmi.security.manager", "yes");
props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");
// Context context=new InitialContext(props);

try {
InitialContext ctx = new InitialContext(props);
SimpleSession simpleSession = (SimpleSession)ctx.lookup("SimpleSessionBean/remote");

String returnString = simpleSession.getEchoString("Ejb3.0 Test Jboss");
System.out.println("sent string:Ejb3.0 Test Jboss"+ ",received string :" + returnString);

} catch (NamingException e) {
e.printStackTrace();
}

}

}


上面代码完成后,把src\com\ejb下的两个类打成jar包,启动服务器,然后把jar包拷贝到C:\jboss-5.1.0.GA\server\default\deploy
打开http://localhost:8080/
JMX Console =>service=JNDIView =>点击list下的Invoke
看到如下说明发布成功:

[img]http://dl.iteye.com/upload/attachment/193666/e2edffba-5ac6-32a7-bace-e66b20f6f355.bmp[/img]

然后运行SimpleSessionClient.java,便可以看到运行结果:
sent string:Ejb3.0 Test Jboss,received string :Ejb3.0 Test Jboss - from session bean
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值