热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

eclipse+JBoss5+EJB3开发指南(1):编写无状态的SessionBean

本文为原创,如需转载,请注明作者和出处,谢谢!本系列教程使用的软件版本如下:Eclipse:3.
本文为原创,如需转载,请注明作者和出处,谢谢!


本系列教程使用的软件版本如下:

Eclipse:3.4.2,
Eclipse IDE for Java EE Developers

JBoss :5.0.1,
http://www.jboss.org/jbossas/downloads/

JDK:1.6.0.14,
http://java.sun.com/javase/downloads/index.jsp



    在本文中将编写一个简单的无状态SessionBean。在发布EJB时,一般需要将EJB程序以jar文件的形式进行发布。这些jar文件将被放在 /server/default/deploy目录中。如果在Eclipse中开发EJB程序,需要进行一些配置。首 选需要在首选项(Preferences)对话框中设置JBoss的安装目录(如D:/jboss5)。然后在运行配置对话框中添加jboss的运行项。 在jboss4.2及以后的版本中,默认情况下jboss只接收来自localhost或127.0.0.1的请求,也就是只接收本地的访问。为了使 jboss接收来自其他地址的请求,在启动jboss时需要使用-b命令行参数进行设置。如下面的启动命令所示:




run.bat -b 200.200.200.123
run.bat -b 
0.0.0.0


    上面的第一行命令表示jboss可以接收来自200.200.200.123的请求。第二条命令表示jboss可以接收来自任意地址的请求。如果不设置-b参数,以非本机方式访问EJB时,JBoss会抛出如下异常:



Exception in thread "main" javax.naming.CommunicationException: Could not obtain connection to any of these urls: 192.168.17.105: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 /192.168.17.105:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server /192.168.17.105:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]]
    at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1725)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:689)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:682)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at test.Client.main(Client.java:29)
    ... ...





一、配置开发环境



    在eclipse中进行开发,也需要设置-b命令行参数。在运行配置对话框中可以按着图1的方式设置-b命令行参数。






图1




二、开发无状态Session Bean



    在Eclipse中开发EJB程序首先需要建立一个EJB Project(工程名为MyEJB)。然后建立一个接口(远程接口)和一个Session Bean。远程接口的代码如下:




package service;

import java.util.List;
import javax.ejb.Remote;
import entity.Greeting;

@Remote
public interface Greeter
{
    
public String greet(String message);
    
public List<Greeting> getAllGreetings();
}



    在上面的代码中使用了&#64;Remote注释将Greeter接口定义为远程接口&#xff0c;也就是在其他的客户端机器上可以通过该接口来访问本地的Session Bean。在Greeter接口中还使用了一个Greeting类&#xff0c;该类的代码如下&#xff1a;


package entity;

import java.io.Serializable;

public class Greeting implements Serializable
{
    
private int id;
    
private String name;
    
public int getId()
    {
        
return id;
    }
    
public void setId(int id)
    {
        
this.id &#61; id;
    }
    
public String getName()
    {
        
return name;
    }
    
public void setName(String name)
    {
        
this.name &#61; name;
    }
}



    要注意的是&#xff0c;由于Greeting类在实例将被传输到客户端&#xff0c;因此&#xff0c;该类需要实现java.io.Serializable接口。

    下面来编写Session Bean&#xff0c;代码如下&#xff1a;


package service;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import entity.Greeting;

&#64;Stateless
public class GreeterBean implements Greeter
{
    &#64;Override
    
public List<Greeting> getAllGreetings()
    {
        
        List
<Greeting> greetings &#61; new ArrayList<Greeting>();
        Greeting greeting 
&#61; new Greeting();
        greeting.setId(
12);
        greeting.setName(
"bill gates");
        greetings.add(greeting);
        greeting 
&#61; new Greeting();
        greeting.setId(
334);
        greeting.setName(
"李宁");
        greetings.add(greeting);
        
return greetings;
    }

    &#64;Override
    
public String greet(String message)
    {
        
return "您好 " &#43; message;
    }

}



    在上面的代码中使用了&#64;Stateless注释将GreeterBean类定义为无状态的Session Bean。如果JBoss正处于启动状态&#xff0c;并保存上面写的类和接口&#xff0c;Eclipse会自动将上面的代码编译&#xff0c;并生成jar文件&#xff0c;发布到jboss的 deploy目录中。该jar文件的目录结构如下&#xff1a;



MyEJB.jar

   entity/Greeting.class

   service/Greeter.class

   service/GreeterBean.class

   META-INF/MANIFEST.MF

   META-INF/jboss.xml



     其中META-INF目录中的两个文件是Eclipse在建立EJB工程时自动生成的&#xff0c;我们不用去管它。读者也可以手工去编译上面的接口和类&#xff0c;并使用jar命令生成jar文件。




三、编写客户端程序



    由于本文使用了远程接口来访问Session Bean&#xff0c;因此&#xff0c;在访问时需要指定EJB所有的机器的IP地址。


package test;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import service.Greeter;

public class Client
{

    public static void main(String[] args) throws Exception
    {
        Properties prop 
&#61; new Properties();

        // 设置相关的属性值

        prop.setProperty(Context.PROVIDER_URL, "192.168.17.105:1099");
        prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
        InitialContext ctx 
&#61; new InitialContext(prop);

        // 开始调用Greeter接口的方法

        Greeter greeter &#61; (Greeter) ctx.lookup("GreeterBean/remote");
        System.out.println(greeter.greet(
"李宁"));
        System.out.println(greeter.getAllGreetings().get(
0).getName());
    }
}



    在上面的代码中&#xff0c;使用了Context.PROVIDER_URL设置了服务端的IP和端口号。

    上面积代码的运行结果如&#xff1a;




您好 李宁
bill gates



    除了在程序中设置属性值外&#xff0c;也可以通过jndi.properties文件进行设置。该文件应放在Eclipse工程的src目录中。该文件的内容如下&#xff1a;




java.naming.factory.initial&#61;org.jnp.interfaces.NamingContextFactory
java.naming.provider.url
&#61;192.168.17.105:1099


    如果使用jndi.properties文件&#xff0c;就不需要在客户端程序中设置相应的属性值了&#xff0c;因此&#xff0c;可以使用如下的代码来调用Session Bean&#xff1a;


package test;

import javax.naming.Context;
import javax.naming.InitialContext;
import service.Greeter;

public class Client
{

    
public static void main(String[] args) throws Exception
    {
        // 不需要在程序中设置相应的属性值
        InitialContext ctx &#61; new InitialContext();
        
// 开始调用Greeter接口的方法
        Greeter greeter 
&#61; (Greeter) ctx.lookup("GreeterBean/remote");
        System.out.println(greeter.greet(
"李宁"));
        System.out.println(greeter.getAllGreetings().get(
0).getName());
    }
}



 
   注意&#xff1a;在编写客户端程序时&#xff0c;需要引用/client目录中的所有jar文件。

下一篇&#xff1a;
eclipse &#43; JBoss 5 &#43; EJB3开发指南&#xff08;1&#xff09;&#xff1a;编写无状态的SessionBean


推荐阅读
author-avatar
小曾
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有