开发环境NetBeans 6.9.1
step 1:GlassFish v3 添加对应的EJB
写一个
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.helloworld.test;
import javax.ejb.EJBException;
/**
*
* @author liuqing
*/
public interface IHelloWorld {
public String sayHelloWorld(String str) throws EJBException;
}
step 2: 在添加本地和远程接口
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.helloworld.test;
import javax.ejb.Local;
/**
*
* @author liuqing
*/
@Local
public interface ILocalHelloWorld extends IHelloWorld{
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.helloworld.test;
import javax.ejb.Remote;
/**
*
* @author liuqing
*/
@Remote
public interface IRemoteHelloWorld extends IHelloWorld{
}
step 3: 添加实现
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.helloworld.test.impl;
import com.helloworld.test.ILocalHelloWorld;
import com.helloworld.test.IRemoteHelloWorld;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
/**
*
* @author liuqing
*/
@Stateless
public class HelloWorldImplBean implements ILocalHelloWorld,IRemoteHelloWorld{
public String sayHelloWorld(String str) throws EJBException {
return "Hello ,My name is EJB Server : " + str;
}
}
step 4: 编号测试类
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.test;
import com.helloworld.test.IRemoteHelloWorld;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
/**
*
* @author liuqing
*/
public class Test {
public static void main(String args[]) throws Exception {
Properties props = new Properties();
props.put("java.naming.factory.initial", "com.sun.enterprise.naming.impl.SerialInitContextFactory");
props.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
props.put("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
//host
props.put("org.omg.CORBA.ORBInitialHost", "localhost");
//EJB Port
props.put("org.omg.CORBA.ORBInitialPort", "3700");
Context ctx = new InitialContext(props);
ctx.lookup("com.helloworld.test.IRemoteHelloWorld");
IRemoteHelloWorld hello = (IRemoteHelloWorld)ctx.lookup(IRemoteHelloWorld.class.getName());
System.out.println(hello.sayHelloWorld("Welcome to GlassFish v3"));
}
}
final 在添加 *.jar 包
glassfish-naming.jar //添加JNDI 包
gf-client-module.jar //客户端模块包
gf-ejb-connector.jar //连接ejb 包
gf-client.jar
运行结果
init: deps-jar: compile-single: run-main: Nov 19, 2010 3:32:39 PM com.sun.enterprise.transaction.JavaEETransactionManagerSimplified initDelegates INFO: Using com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate as the delegate Hello ,My name is EJB Server : Welcome to GlassFish v3 BUILD SUCCESSFUL (total time: 7 seconds)