JBuider下配置JBoss并部署、测试EJB

今天几乎一直对着电脑,上午上 SAS 课,吃完饭又在机房做相关 SAS 练习,直到下午 5 点才从机房出来,这玩意还真不好搞,都搞不清楚其具体的作用,只好慢慢来吧。晚上跟哥们出去吃饭,喝了些酒,虽然大脑有点发晕,但写这些,还是蛮清醒的:
笔者以 Jbuilder2006 Jboss4.0.2 为运行环境,在该环境下开发 EJB 并进行部署和测试 EJB, 以下为其具体过程:
1、 安装 JBoss4.0.2 ,安装很简单,只解压 JBoss 安装包即可;
2、  如果配置好 java 的安装目录的话,此部可省略。否则必须配置 java 的安装目录;
3、  测试jboss是否安装成功:方法是:启动jboss,在地址栏中输入: http://localhost:8080/jmx-console/
4、  在JBulidler2006下配置Jboss服务器:
l         配置Jboss服务器
l         修改工程默认属性,服务器为所配置的JBoss
5、  在JBuilder2006下开发EJB:
l         新建工程
l         新建EJB Module
l         添加EJB,增加Field和Method
6、  编译整个工程
7、  发布EJB:选中EJBModule,右键点击deploy进行发布
8、  右键点击,选中Run using EJBModule;或者可以通过Run菜单下的Configuration进行配置,然后直接run project;
9、  如果没有问题,表示EJB部署成功,否则,则需进行具体修改;
10、              是不可以直接访问,必须建立EJB具体客户端:比如可以是一个Servlet、一个独立的Java应用、另一个EJB,还可以是一个移动设备……EJB
以下是 JBuilder2006 自生成的 ejb Client 代码,供参考:
package  testejb;

import  javax.naming. * ;
import  javax.rmi.PortableRemoteObject;
import  javax.naming.InitialContext;
import  java.util.Hashtable;

public   class  TestPrintTestClient1  {
  
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 TestPrint testPrint = null;
  
private TestPrintHome testPrintHome = null;

  
//Construct the EJB test client
  public TestPrintTestClient1() {
    initialize();
  }


  
public void initialize() {
    
long startTime = 0;
    
if (logging) {
      log(
"Initializing bean access.");
      startTime 
= System.currentTimeMillis();
    }


    
try {

      
//get naming context
      Context context = getInitialContext();
      
//look up jndi name
      Object ref = context.lookup("TestPrint");
      
//look up jndi name and cast to Home interface
      testPrintHome = (TestPrintHome) PortableRemoteObject.narrow(ref,
          TestPrintHome.
class);
      
if (logging) {
        log(
"Succeeded initializing bean access through Home interface.");
        
long endTime = System.currentTimeMillis();
        log(
"Execution time: " + (endTime - startTime) + " ms.");
      }


    }

    
catch (Exception e) {
      
if (logging) {
        log(
"Failed initializing bean access.");
      }

      e.printStackTrace();
    }


  }


  
private Context getInitialContext() throws NamingException {
    Hashtable environment 
= new Hashtable();

    environment.put(Context.INITIAL_CONTEXT_FACTORY,
                    
"org.jnp.interfaces.NamingContextFactory");
    environment.put(Context.URL_PKG_PREFIXES,
                    
"org.jboss.naming:org.jnp.interfaces");
    environment.put(Context.PROVIDER_URL, 
"jnp://localhost:1099");

    
return new InitialContext(environment);
  }


  
//----------------------------------------------------------------------------
  
// Methods that use Home interface methods to generate a Remote interface reference
  
//----------------------------------------------------------------------------

  
public TestPrint create() {
    
long startTime = 0;
    
if (logging) {
      log(
"Calling create()");
      startTime 
= System.currentTimeMillis();
    }


    
try {
      testPrint 
= testPrintHome.create();
      
if (logging) {
        log(
"Succeeded: create()");
        
long endTime = System.currentTimeMillis();
        log(
"Execution time: " + (endTime - startTime) + " ms.");
      }

    }

    
catch (Exception e) {
      
if (logging) {
        log(
"Failed : create()");
      }

      e.printStackTrace();
    }

    
if (logging) {
      log(
"Return value from create(): " + testPrint + ".");
    }

    
return testPrint;
  }


  
//----------------------------------------------------------------------------
  
// Methods that use Remote interface methods to access data through the bean
  
//----------------------------------------------------------------------------

  
public void pringHello() {
    
if (testPrint == null{
      System.out.println(
"Error in pringHello(): " + ERROR_NULL_REMOTE);
      
return;
    }

    
long startTime = 0;
    
if (logging) {
      log(
"Calling pringHello()");
      startTime 
= System.currentTimeMillis();
    }


    
try {
      testPrint.pringHello();
      
if (logging) {
        log(
"Succeeded: pringHello()");
        
long endTime = System.currentTimeMillis();
        log(
"Execution time: " + (endTime - startTime) + " ms.");
      }

    }

    
catch (Exception e) {
      
if (logging) {
        log(
"Failed : pringHello()");
      }

      e.printStackTrace();
    }

  }


  
//----------------------------------------------------------------------------
  
// Utility Methods
  
//----------------------------------------------------------------------------

  
private void log(String message) {

    
if (message == null{
      System.out.println(
"-- null");
    }

    
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) {
    TestPrintTestClient1 client 
= new TestPrintTestClient1();
    
// 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.
  }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值