EJB2.0의 HelloWorld 버전

12397 단어
개발 도구:javaee 버전의 eclipse
jboss4.0、jdk1.6
테스트 결과 jb2,0은 jboss5로 실행할 수 없을 것 같아서 jboss4로 변경했습니다.
javaee 버전의 eclipse로 jboss4를 설정합니다.
새 jb 프로젝트를 만듭니다. jboss4와 jb2.를 선택하십시오.0.
그런 다음 자바 코드를 추가합니다.
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 디렉토리에서 파일 jb-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>

그런 다음 런 온 서버를 실행합니다.
테스트 코드 작성:
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

좋은 웹페이지 즐겨찾기