EJB2.0之HelloWorld版本

开发工具:javaee版本的eclipse

jboss4.0、jdk1.6


经测试,ejb2,0似乎不能用jboss5来运行,所以改用jboss4。


用javaee版本的eclipse配置好jboss4。


新建ejb项目,选择jboss4和 ejb2.0。


然后加入java代码:

package com.ejb;

import java.rmi.RemoteException;

import javax.ejb.EJBObject;

/**
 * 所有的业务方法都要在此接口中声明
 */
public interface Hello extends EJBObject {
	/**
	 * 业务方法,组件接口中的业务方法必须抛出RemoteException
	 * @param someOne
	 * @return
	 * @throws RemoteException
	 */
	public String sayHello(String someOne) throws RemoteException;
}

package com.ejb;

import java.rmi.RemoteException;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import static java.lang.System.out;

/**
 * 所有具体的业务逻辑都在此类里面,此类不抛出远程异常
 */
public class HelloBean implements SessionBean {

	private static final long serialVersionUID = 1L;

	@Override
	public void ejbActivate() throws EJBException, RemoteException {
		out.println("ejbActivate");
	}

	@Override
	public void ejbPassivate() throws EJBException, RemoteException {
		out.println("ejbPassivate");
	}

	@Override
	public void ejbRemove() throws EJBException, RemoteException {
		out.println("ejbRemove");
	}

	@Override
	public void setSessionContext(SessionContext arg0) throws EJBException,
			RemoteException {
		out.println("setSessionContext");
	}

	public void ejbCreate() {
		out.println("ejbCreate");
	}

	/**
	 * 必须有这个方法,但是这个方法不是来自SessionBean接口的
	 * @param someOne
	 * @return
	 */
	public String sayHello(String someOne) {
		out.println("sayHello");
		return "Hello, " + someOne + "!";
	}

}

package com.ejb;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBHome;

/**
 * Home接口必须扩展EJBHome或者EJBLocalHome接口
 * 客户使用Home接口来请求组件接口的一个引用
 * 可以将Home接口看做是一个工厂,它能制造Bean的引用个,而且能向客户分配bean引用
 */
public interface HelloHome extends EJBHome {
	public Hello create() throws CreateException, RemoteException;
}

在META-INF目录,修改文件ejb-jar.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 
                'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
	<display-name>Hello EJB</display-name>
	<enterprise-beans>
		<session>
			<display-name>helloEJB</display-name>
			<ejb-name>helloEJB</ejb-name>
			<home>com.ejb.HelloHome</home>
			<remote>com.ejb.Hello</remote>
			<ejb-class>com.ejb.HelloBean</ejb-class>
			<session-type>Stateless</session-type>
			<transaction-type>Bean</transaction-type>
			<security-identity>
				<description></description>
				<use-caller-identity></use-caller-identity>
			</security-identity>
		</session>
	</enterprise-beans>
</ejb-jar>

增加文件jboss.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jboss>
	<enterprise-beans>
		<session>
			<ejb-name>helloEJB</ejb-name>
			<jndi-name>ejb/helloEJB</jndi-name>
		</session>
	</enterprise-beans>
</jboss>

然后运行Run on Server。


编写测试代码:

package com.ejb.client;

import java.rmi.RemoteException;
import java.util.Properties;

import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import com.ejb.Hello;
import com.ejb.HelloHome;

public class TestClient {

	/**
	 * @param args
	 * @throws NamingException
	 * @throws CreateException 
	 * @throws RemoteException 
	 */
	public static void main(String[] args) throws NamingException, RemoteException, CreateException {
		Properties props = new Properties();
		props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
		props.setProperty("java.naming.provider.url", "localhost:1099");
		// 初始化JNDI上下文环境,因为客户端不知道JBOSS的环境
		InitialContext ctx = new InitialContext(props);
		// 检索指定的对象
		Object objref = ctx.lookup("ejb/helloEJB");
		// 强制转换为所需类型的对象
		HelloHome home = (HelloHome) PortableRemoteObject.narrow(objref, HelloHome.class);
		// 通过home对象创建一个组件接口对象
		Hello hello = home.create();
		// 通过组件接口对象调用业务方法
		String msg = hello.sayHello("ejb");
		System.out.println(msg);

	}

}

客户端输出:

Hello, ejb!

服务端输出:

14:41:33,515 INFO  [Server] Starting JBoss (MX MicroKernel)...
14:41:33,515 INFO  [Server] Release ID: JBoss [Zion] 4.0.0 (build: CVSTag=JBoss_4_0_0 date=200409200418)
14:41:33,515 INFO  [Server] Home Dir: F:\software\jboss-4.0.0
14:41:33,515 INFO  [Server] Home URL: file:/F:/software/jboss-4.0.0/
14:41:33,515 INFO  [Server] Library URL: file:/F:/software/jboss-4.0.0/lib/
14:41:33,515 INFO  [Server] Patch URL: null
14:41:33,515 INFO  [Server] Server Name: default
14:41:33,515 INFO  [Server] Server Home Dir: F:\software\jboss-4.0.0\server\default
14:41:33,515 INFO  [Server] Server Home URL: file:/F:/software/jboss-4.0.0/server/default/
14:41:33,515 INFO  [Server] Server Data Dir: F:\software\jboss-4.0.0\server\default\data
14:41:33,515 INFO  [Server] Server Temp Dir: F:\software\jboss-4.0.0\server\default\tmp
14:41:33,515 INFO  [Server] Server Config URL: file:/F:/software/jboss-4.0.0/server/default/conf/
14:41:33,515 INFO  [Server] Server Library URL: file:/F:/software/jboss-4.0.0/server/default/lib/
14:41:33,515 INFO  [Server] Root Deployment Filename: jboss-service.xml
14:41:33,515 INFO  [Server] Starting General Purpose Architecture (GPA)...
14:41:33,765 INFO  [ServerInfo] Java version: 1.6.0_41,Sun Microsystems Inc.
14:41:33,765 INFO  [ServerInfo] Java VM: Java HotSpot(TM) Client VM 20.14-b01,Sun Microsystems Inc.
14:41:33,765 INFO  [ServerInfo] OS-System: Windows XP 5.1,x86
14:41:33,984 INFO  [Server] Core system initialized
14:41:35,359 INFO  [Log4jService$URLWatchTimerTask] Configuring from URL: resource:log4j.xml
14:41:35,406 INFO  [WebService] Using RMI server codebase: http://notice.ppstream.com:8083/
14:41:35,562 INFO  [NamingService] Started jnpPort=1099, rmiPort=1098, backlog=50, bindAddress=/0.0.0.0, Client SocketFactory=null, Server SocketFactory=org.jboss.net.sockets.DefaultSocketFactory@ad093076
14:41:36,796 INFO  [Embedded] Catalina naming disabled
14:41:37,125 INFO  [Http11Protocol] Initializing Coyote HTTP/1.1 on http-0.0.0.0-8080
14:41:37,156 INFO  [Catalina] Initialization processed in 297 ms
14:41:37,156 INFO  [StandardService] Starting service jboss.web
14:41:37,156 INFO  [StandardEngine] Starting Servlet Engine: Apache Tomcat/5.0.28
14:41:37,171 INFO  [StandardHost] XML validation disabled
14:41:37,171 INFO  [Catalina] Server startup in 15 ms
14:41:37,265 INFO  [TomcatDeployer] deploy, ctxPath=/ebxmlrr, warUrl=file:/F:/software/jboss-4.0.0/server/default/deploy/ebxmlrr-service.sar/ebxmlrr.war/
14:41:37,640 INFO  [WebappLoader] Dual registration of jndi stream handler: factory already defined
14:41:37,796 INFO  [STDOUT] Initialized REST
14:41:37,859 INFO  [SAAJServlet] init
14:41:38,093 INFO  [SAAJServlet] init
14:41:38,187 INFO  [TomcatDeployer] deploy, ctxPath=/ws4ee, warUrl=file:/F:/software/jboss-4.0.0/server/default/tmp/deploy/tmp4665125682164258987jboss-ws4ee-exp.war/
14:41:38,265 INFO  [TomcatDeployer] deploy, ctxPath=/, warUrl=file:/F:/software/jboss-4.0.0/server/default/deploy/jbossweb-tomcat50.sar/ROOT.war/
14:41:38,671 INFO  [STDOUT] [ jacorb.home unset! Will use '.' ]
14:41:38,671 INFO  [STDOUT] [ File .\jacorb.properties for configuration jacorb not found ]
14:41:38,828 INFO  [interceptors] InterceptorManager started with 2 SIs, 2 CIs and 4 IORIs
14:41:39,015 INFO  [orb] ORB run
14:41:39,093 INFO  [CorbaNamingService] Naming: [IOR:000000000000002B49444C3A6F6D672E6F72672F436F734E616D696E672F4E616D696E67436F6E746578744578743A312E3000000000000200000000000000E8000102000000000E3139322E3136382E312E313030000DC8000000114A426F73732F4E616D696E672F726F6F74000000000000060000000000000008000000004A414300000000010000001C0000000000010001000000010501000100010109000000010501000100000014000000080000001A00000DC9000000210000005000000000000000010000000000000024000000200000007E00000000000000010000000E3139322E3136382E312E313030000DC9000000000000000000000000000000000000000000000000000000000000002000000004000000000000001F000000040000000300000001000000440000000000000003000000010000001C000000000001000100000001050100010001010900000001050100010000002000000004000000000000001F0000000400000003]
14:41:39,187 INFO  [naming] re-Bound name: TransactionService
14:41:39,187 INFO  [CorbaTransactionService] TransactionFactory: [IOR:000000000000003049444C3A6F72672F6A626F73732F746D2F69696F702F5472616E73616374696F6E466163746F72794578743A312E30000000000200000000000000E8000102000000000E3139322E3136382E312E313030000DC8000000144A426F73732F5472616E73616374696F6E732F46000000060000000000000008000000004A414300000000010000001C0000000000010001000000010501000100010109000000010501000100000014000000080000001A00000DC9000000210000005000000000000000010000000000000024000000200000007E00000000000000010000000E3139322E3136382E312E313030000DC9000000000000000000000000000000000000000000000000000000000000002000000004000000000000001F000000040000000300000001000000440000000000000003000000010000001C000000000001000100000001050100010001010900000001050100010000002000000004000000000000001F0000000400000003]
14:41:39,187 INFO  [naming] re-Bound name: UserTransaction
14:41:39,578 INFO  [RARDeployment] Required license terms exist view the META-INF/ra.xml: file:/F:/software/jboss-4.0.0/server/default/deploy/jboss-local-jdbc.rar
14:41:39,640 INFO  [RARDeployment] Required license terms exist view the META-INF/ra.xml: file:/F:/software/jboss-4.0.0/server/default/deploy/jboss-xa-jdbc.rar
14:41:39,687 INFO  [RARDeployment] Required license terms exist view the META-INF/ra.xml: file:/F:/software/jboss-4.0.0/server/default/deploy/jms/jms-ra.rar
14:41:40,375 INFO  [WrapperDataSourceService] Bound connection factory for resource adapter for ConnectionManager 'jboss.jca:service=DataSourceBinding,name=DefaultDS to JNDI name 'java:DefaultDS'
14:41:40,515 INFO  [ConnectionFactoryBindingService] Bound connection factory for resource adapter for ConnectionManager 'jboss.jca:service=ConnectionFactoryBinding,name=JmsXA to JNDI name 'java:JmsXA'
14:41:40,750 INFO  [A] Bound to JNDI name: queue/A
14:41:40,750 INFO  [B] Bound to JNDI name: queue/B
14:41:40,750 INFO  [C] Bound to JNDI name: queue/C
14:41:40,750 INFO  [D] Bound to JNDI name: queue/D
14:41:40,765 INFO  [ex] Bound to JNDI name: queue/ex
14:41:40,828 INFO  [testTopic] Bound to JNDI name: topic/testTopic
14:41:40,828 INFO  [securedTopic] Bound to JNDI name: topic/securedTopic
14:41:40,828 INFO  [testDurableTopic] Bound to JNDI name: topic/testDurableTopic
14:41:40,828 INFO  [testQueue] Bound to JNDI name: queue/testQueue
14:41:40,828 INFO  [DLQ] Bound to JNDI name: queue/DLQ
14:41:40,890 INFO  [UILServerILService] JBossMQ UIL service available at : /0.0.0.0:8093
14:41:41,078 INFO  [MailService] Mail Service bound to java:/Mail
14:41:41,250 INFO  [EjbModule] Deploying helloEJB
14:41:41,343 INFO  [EJBDeployer] Deployed: file:/F:/software/jboss-4.0.0/server/default/deploy/ejb2.jar
14:41:41,375 INFO  [TomcatDeployer] deploy, ctxPath=/jmx-console, warUrl=file:/F:/software/jboss-4.0.0/server/default/deploy/jmx-console.war/
14:41:41,500 INFO  [TomcatDeployer] deploy, ctxPath=/web-console, warUrl=file:/F:/software/jboss-4.0.0/server/default/deploy/management/web-console.war/
14:41:42,156 INFO  [Http11Protocol] Starting Coyote HTTP/1.1 on http-0.0.0.0-8080
14:41:42,218 INFO  [ChannelSocket] JK2: ajp13 listening on /0.0.0.0:8009
14:41:42,218 INFO  [JkMain] Jk running ID=0 time=0/15  config=null
14:41:42,234 INFO  [Server] JBoss (MX MicroKernel) [4.0.0 (build: CVSTag=JBoss_4_0_0 date=200409200418)] Started in 8s:610ms
14:42:24,187 INFO  [STDOUT] setSessionContext
14:42:24,187 INFO  [STDOUT] ejbCreate
14:42:24,187 INFO  [STDOUT] sayHello


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值