JBoss 4.0 EJB deployment

Source: Internet
Author: User
Tags jboss server

JBoss 4.0 EJB helloworld

Development Environment: Java SDK 1.4.2, JBoss 4.0, Windows 2003

The development of sessionbean EJB requires at least three classes, remote interface, home interface, and bean implementation (bean behavior ).

1. remote interface is used to reveal some external methods of EJB.

Package helloworld; import javax. EJB. ejbobject;

Import java. RMI. RemoteException;
Public interface remotehello extends ejbobject
{Public String helloecho (string inputstring) throws RemoteException ;}
2. The home interface is used to specify how to create a bean to implement remote interface.
Package helloworld;
Import java. Io. serializable;
Import java. RMI. RemoteException;
Import javax. EJB. createexception;
Import javax. EJB. ejbhome;
Public interface homehello extends ejbhome

{Remotehello create () throws RemoteException, createexception ;}

3. Bean implementation is the implementation of the provided methods. These methods are specified in both interfaces.

Package helloworld;

Import java. RMI. RemoteException;

Import javax. EJB. sessionbean;

Import javax. EJB. sessioncontext;

Public class hellobean implements sessionbean
{

Public String helloecho (string inputstring)
{

System. Out. println ("someone called 'Hello echo successed! '");

Return "**********" + inputstring + "*********";}

/** Empty method body */

Public void ejbcreate (){

System. Out. println ("EJB 4 is creating !... ");}

/** Every ejbcreate () method always needs a corresponding ejbpostcreate () method with exactly the same parameter types .*/

Public void ejbpostcreate (){}

/** Empty method body */

Public void ejbremove (){

System. Out. println ("EJB 4 is removing !... ");}

/** Empty method body */

Public void ejbactivate (){

System. Out. println ("EJB 4 is activating !... ");}

/** Empty method body */

Public void ejbpassivate ()

{}

/** Empty method body */

Public void setsessioncontext (sessioncontext SC)

{}

}

Deploy jar

These classes must be packaged into a jar file, which contains the directory structure and package hierarchy. in this example, these classes are in the package helloworld so that they need to be in the directory helloworld.

Deploy the release descriptor ejb-jar.xml and JBoss. xml
Before creating the JAR file, you also need a directory named META-INF, which stores the deployment Describer (usually called 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>
<Description> neosue helloworld sample application </description>
<Display-Name> helloworld EJB </display-Name>
<Enterprise-beans>
<Session>
<EJB-Name> helloworld </EJB-Name>
<! -- Home interface -->
<Home> helloworld. homehello </Home>
<! -- Remote interface -->
<Remote> helloworld. remotehello </remote>
<! -- Bean implementation -->
<EJB-class> helloworld. hellobean </EJB-class>
<Session-type> stateless </session-type>
<Transaction-type> bean </transaction-type>
</Session>
</Enterprise-beans>
</EJB-jar>

JBoss. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype JBoss public "-// JBoss // DTD JBoss 4.0 // en ""
Http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd

">
<JBoss>
<Enterprise-beans>
<Session>
<EJB-Name> helloworld </EJB-Name>
<JNDI-Name> helloworld/Hello </JNDI-Name>
</Session>
</Enterprise-beans>
</JBoss>
Although you have configured the above application and the JNDI name, once deployed and published to the JBoss server, you still need a JNDI. properties file to tell the client request that calls your program where to initialize the JNDI Naming Service.

Test procedure:

Import javax. Naming. context;

Import javax. Naming. initialcontext;

Import javax. RMI. portableremoteobject;

Import helloworld .*;

Public class mytest {

Public static void main (string [] ARGs ){

Try {context CTX = new initialcontext ();

Object ref = CTX. Lookup ("helloworld/Hello ");

Homehello home = (homehello) portableremoteobject. Narrow (ref, homehello. Class );

Remotehello user = home. Create (); system. Out. println (user. helloecho ("so easy! "));

} Catch (exception e ){

E. printstacktrace ();

}

}

}

JBoss EJB deployment steps to establish remote interface --> home interface --> and bean implementation --> ejb-jar.xml --> JBoss. XML -- package (package [Jar CVF packagename. jar.]) --> copy to the JBoss deploy directory.

---------------------------------------------------------
Original URL
Http://blog.blogchina.com/refer.159508.html


---------------------------------------------------------
Note:

OS: Windows 2000;
JDK: 1.5.0rc;
JBoss: 4.0


Helloworld. Jar
| -- META-INF
| -- JBoss. xml
| -- Ejb-jar.xml
| -- Manifest. MF (automatically generated)
| -- Helloworld
| -- Remotehello. Class
| -- JNDI. Properties
| -- Homehello. Class
| -- Hellobean. Class

In the test file mytest. Class, copy a copy of The JNDI. properties file in the same directory.

JNDI. Properties
The content is as follows:


Java. Naming. Factory. Initial = org. jnp. Interfaces. namingcontextfactory
Java. Naming. provider. url = localhost: 1099
Java. Naming. Factory. url. pkgs = org. JBoss. Naming: org. jnp. Interfaces



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.