Introduction to EJB in JBoss

Source: Internet
Author: User
Introduction to EJB in JBoss

1. JBossIntroduction

JBossIs a running EJBJ2EEApplication Server. It is an open-source project that follows the latest J2EESpecification. From JBossThe project has been started from an EJBContainer becomes a J2EE-basedA WebOperating System(Operating system for Web), It reflects the J2EEThe latest technology in the specification, and it is still in the Java& Ldquo; best JavaApplication Server& Rdquo; grand prize. Whether it is learning or application, JBossIt provides us with a very good platform. About JBossFor more information, see its home page http://www.jboss. Org.

Start using JBossPerform EJBDuring development, there is a difficult initial stage because there are not many resources available for reference. JBossThe configuration and use of does not provide a graphical wizard interface, so the development and deployment of EJBRelatively complex. This article demonstrates JBoss as much as possibleEJB in section 3.0A Brief Introduction to development and deployment, so that JBoss can be used at the beginningUsers can quickly access the real J2EEApplication Development in progress.

This article mainly introduces JBossDifferent types of ejbs in 3.0Configuration and deployment of not basic EJBDevelopers make too many descriptions, so we hope readers can have J2EEAnd EJBExperience. For more information, see reference 1.

2. JBoss3.0 basic EJBConfiguration and deployment
Based on J2EEStandard requirement, a basic EJBThe description file used by the jar package is EJB.-Jar. xml. WebThe war package of the application uses Web. Xml. The enterprise application's ear package uses application. xml.. These configuration files are neutral and platform-independent. Application ServerYou can use other configuration files to describe specific servers.. In JBossSuch files include JBoss. Xml, JBoss-Web. Xml. JBossThese files are not required in the container. If JBoss is provided. XmlAnd EJB-Jar. xmlPut it in the same directory, JBoss-Web. XmlAnd Web. XmlIn the same directory. About JBoss. XmlAnd JBoss-Web. XmlFor more information, see JBoss.Install the corresponding DTD file under the docs/DTD/directory.

2.1 JBossAbout EJBClient Configuration:
Call EJBThe client can be a JSP, ServletOr a client application. If the client and serverNot in the same JavaOn the VM, the client must provide a JNDI. properties file to inform the client about the JNDI Naming Service and set the directory where the file is located to the environment variable classpath.

The following is an example of JNDI. properties:

Java. Naming. Factory. Initial = org. jnp. Interfaces. namingcontextfactory
Java. Naming. provider. url = localhost: 1099 (ServerAddress and port number)
Java. Naming. Factory. url. pkgs = org. JBoss. Naming: org. jnp. Interfaces
Through this file and JBossSome client class libraries, you can use JBossProvided EJBObject Service.

2.2 JBossDeploy EJB in:
First introduce JBossDeploy various ejbs in containersObject. The following example uses Session Bean as an example. The client is a WebApplication. The demo program is not described here.

Method 1:

Use the developed EJBJar package and WebThe war package of the application is stored in the deploy directory.

Demonstration 1: JBoss is not required. XmlFile and any other special settings.

EJB-Jar. xml:

<ejb-jar>

       &middot;&middot;&middot;&middot;&middot;&middot;

              <ejb-name>ejbtest</ejb-name>

       &middot;&middot;&middot;&middot;&middot;&middot;

</ejb-jar>

Client: WebFor Web. XmlNo need to add EJBInformation, no JBoss required-Web. XmlFile. Reference EJBJSP of the objectThe file is as follows:

& Middot;

Initialcontext CTX = new initialcontext ();

Object objref = CTX. Lookup (& quot; EJBTest & quot;); // use <EJB-Name> beanhome = (zcxejb1 home) portableremoteobject. Narrow (objref, EJBTesthome. Class );

& Middot;

Demonstration 2: Use JBoss. XmlFile.

By default, JBossThrough the EJB-Jar. xml<EJB-Name> XXX </EJB-Name> XXX to use EJBHome interface. However, if multiple ejbs existObjects in the same EJBIn the jar package-Jar. xml<EJB-Name> XXX </EJB-Name> may not indicate an EJB.Object, so we generally want to provide additional information, such as using the format & quot; [Application name]/[bean name] & quot; to reference an EJBObject. At this time, the JNDI service may not be able to find your EJB correctly.Object, we need to use JBoss. XmlFile to implement the JNDI name to the EJBName Redirection. This file must be associated with EJB-Jar. xmlPut them together in the meta-info directory.

EJB-Jar. xml:

<ejb-jar>

       &middot;&middot;&middot;&middot;&middot;&middot;

       <ejb-name>ejbtest</ejb-name>

       &middot;&middot;&middot;&middot;&middot;&middot;

</ejb-jar>

JBoss. Xml:

<jboss>

       <ejb-name>ejbtest</ejb-name>

       <jndi-name>example/ejbtest</jndi-name>

</jboss>

Client: use the new JNDI name for EJBLocate.

& Middot;

Initialcontext CTX = new initialcontext ();

Object objref = CTX. Lookup (& quot; example/EJBTest & quot;); // use <JNDI-Name> beanhome = (zcxejb1 home) portableremoteobject. Narrow (objref, EJBTesthome. Class );

& Middot;

Method 2:

Set EJBAnd WebThe application is packaged into an enterprise application package. When deploying the ear, you can simply use application. xmlDescription EJBJar package and WebThe war package of the application, also in the Web. War can be used in the same way as the first method without providing special informationObject. The client code does not need to be modified. Here, we will not give an example.

Webcall EJB:

J2EE application/
|__ EJB component/(haiejb. Jar)
| |__ META-INF/
| |__ Ejb-jar.xml
| |__ JBoss. xml
| |__ Ejbs/
| |__ Haihome. Class
| |__ Haiclient. Class
| |__ Haibean. Class
|__ Web application/(haiejb. War)
| |__ Haiejb. jsp
| |__ WEB-INF/
| |__ Web. xml
| |__ Jboss-web.xml
|__ META-INF/
|__ Application. xml

In addition, we can perform further deployment. In this case, we need to use the Web. XmlAnd JBoss-Web. Xml. JBoss-Web. XmlYes JBossProvidesThe file configured by the application. JBoss-Web. XmlAnd Web. XmlPut together on WebApplication Web-INF directory.

Demonstration 1: modifying the Web. XmlFile, add <EJB-Ref> mark, not using JBoss-Web. XmlFile.

(Note <EJB-Ref> includes internal and external references. For the same unit, you can use <EJB-Link> reference directly without providing other information .)

Web. Xml:

& Middot;

<EJB-Ref>

<EJB-Ref-Name> EJB/EJBTest </EJB-Ref-Name> <! -- Use the naming method recommended by Sun -->

<EJB-Ref-type> session </EJB-Ref-type>

<Home> org. zcx. Test. zcxejb1 home </Home>

<Remote> org. zcx. Test. zcxejb1 </remote>

<EJB-Link> EJBTest </EJB-Link> <! -- Must be used with the EJB-Name match -->

</EJB-Ref>

& Middot;

Client: Because. XmlIntroduce EJBReference description in this case, EJBLocation changed:

& Middot;

Initialcontext CTX = new initialcontext ();

Object objref = CTX. Lookup (& quot; Java: COMP/ENV/EJB/EJBTest & quot;); // use Java: COMP/ENV namespace beanhome = (zcxejb1 home) portableremoteobject. Narrow (objref, EJBTesthome. Class );

& Middot;

Demonstration 2: joint use of web. XmlAnd JBoss-Web. Xml

Web. Xml

& Middot;

<EJB-Ref>

<EJB-Ref-Name> EJB/EJBTest </EJB-Ref-Name> <! -- Use the naming method recommended by Sun -->

<EJB-Ref-type> session </EJB-Ref-type>

<Home> org. zcx. Test. zcxejb1 home </Home>

<Remote> org. zcx. Test. zcxejb1 </remote>

</EJB-Ref>

& Middot;

JBoss-Web. Xml

<EJB-Ref>

<EJB-Ref-Name> EJB/EJBTest </EJB-Ref-Name>

<JNDI-Name> example/EJBTest </JNDI-Name> <! -- Corresponding to EJBThe JNDI name of the object -->

</EJB-Ref>

Client:

       nitialcontext ctx = new initialcontext();

       object objref  = ctx.lookup(&quot;java:comp/env/ ejb/ejbtest &quot;);                                           beanhome=(zcxejb1home)portableremoteobject.narrow(objref,ejbtesthome.class);

All the above demos illustrate EJB-Jar. xml, JBoss. Xml, Web. Xml, JBoss-Web. XmlThe basic relationship between them and how they are used. They are used to deploy various types of ejbs.Key file of the object. Next we will introduce different types of ejbs.The special configurations required by the object.

3. JBossRelated configuration of cmp2.0 Entity Bean in 3.0
JBossJBoss in June 3.0The CMP engine implements the EJB2.0 cmp2.0 specification. JBossIn versions earlier than 3.0, the CMP engine is Jaws, which uses standardjaws. xmlAnd Jaws. xml. In the new JBossJBoss in CMP Engine3.0 pass standardjbossCmp-jdbc.xmlAnd josscmp-jdbc.xmlTo configure Entity Bean. JBoss3.0 first process standardjbossCmp-jdbc.xmlThenWhether JBoss is provided in jarCmp-jdbc.xmlFor further processing. Use standardjbossCmp-jdbc.xmlAnd JBossCmp-jdbc.xmlThe following functions can be provided for Entity Bean deployment:

L specify the data source and corresponding type ing used

L specify attributes that the engine cares about

L specify how the engine creates and manages DatabasesTable Principles

L description of finder and EJBSelect Method

L specifying the ing between attributes and fields

This is mainly based on standardjbossCmp-jdbc.xml/JBossCmp-jdbc.xmlThis section describes how to configure cmp2.0. JBossThe original jaws engine configuration method and the new JBossThe CMP configuration method is similar. For more information about these files, see JBoss.The corresponding DTD file in the installation path/docs/DTD.

3.1 add new data sources
JBossBuilt-in DatabaseIs a hypersonic Database, We can add some new data sources. The procedure is simple. For details, refer to JBoss.*-Service. XML in the/docs/examples/JCA directoryFile to find the corresponding type of DatabaseConfiguration sample file. Take MySQL-Service. xmlFile as an example, it can configure a MySQLDatabaseAs the data source of Entity Bean. The following example shows how to create a MySQLDS MySQLData source.

Figure 1 shows the configured MySQL-Service. xmlA code sample of the file. You can customize the data source name and other attributes related to the connection through attribute settings. Note that all name attributes in the file must be consistent.

Figure 1 MySQL-Servic. xmlSample Code

After this file is configured, deploy it in JBossUnder the deploy directory, the corresponding JDBC driver is also under the lib directory. Restart JBossThe new data source is loaded.

3.2 Entity Bean's use of data sources
Method 1:

Change standardjboss directlyCmp-jdbc.xml. Add the original data source Java:/Defautlds block the use of the new data source Java:/MySQLDS. (JavaThe prefix is required .) You do not need to change it elsewhere. JBossThe new data source is used as the default data source. Figure 2 shows a configured file segment.

Figure 2 standardjbossCmp-jdbc.xmlSample Code

Method 2:

Since different entity beans may use different data sources, rather than using the same default configuration, you must provide support for customized data source information for separate entity beans. Standardjboss does not need to be modified.Cmp-jdbc.xmlFile, you can add a new deployment file JBoss under the meta-info directory in the jar package of Entity BeanCmp-jdbc.xml. This file can be used to describe the configuration information about the data source of a specific entity bean. Figure 3 shows a configured file segment.

Figure 3 JBossCmp-jdbc.xmlSample Code

4. JBossMessage Driven bean configuration in 3.0
In JBossMDB development requires the use of JMSFunction. JMSIt is a message-oriented middleware API developed by Sun. Its main purpose is to create a unified message-oriented JavaAPI to avoid using the provider-specific API. There are several different kinds of JMSThe system is available. JBossJBoss is provided in 3.0.MQ.

4.1 JBossConfigure JMS in 3.0Service
In JBoss3.0 and JMSService-related configuration files include two files: JBossMq-service.xmlAnd JBossMq-destinations-service.xml. JBossMq-service.xmlJBoss configuredCore object information in the MQ service. Generally, we do not need to process this configuration file. JBossMq-destinations-service.xmlDefines the destination information required by a specific application. By editing it, we can create a new topic and queue for the specific JMSThe destination of the application. In JBossThe steps for creating a specific topic and queues for an application are relatively simple. Refer to JBossMq-destinations-service.xmlFile Format: add the topic name mytestapptopic and queue name mytestappqueue required by your application to the file. 4.

Figure 4 JBossMq-destinations-service.xmlSample Code

4.2 JBossEJB in 3.0Use JMSService configuration
When developing mdb, you must specify the JMS used in the deployment file.The destination information in the service. JBossThrough EJB-Jar. xmlAnd JBoss. XmlDescription. In JBoss. Xml<Destination-JNDI-Name> is used to mark the destination information used.

EJB-Jar. xml

<message-driven>

      <ejb-name>hellotopicmdb</ejb-name>

      <ejb-class>org.zcx.test.hellomdb</ejb-class>

      <message-selector></message-selector>

      <transaction-type>container</transaction-type>

      <message-driven-destination>

        <destination-type>javax.jms.topic</destination-type>

        <subscription-durability>nondurable</subscription-durability>

      </message-driven-destination>

</message-driven>

JBoss. Xml

  <message-driven>

      <ejb-name>hellotopicmdb</ejb-name>

      <configuration-name>standard message driven bean</configuration-name>

      <destination-jndi-name>topic/myapptesttopic</destination-jndi-name>

    </message-driven>

Client:

&middot;&middot;&middot;&middot;&middot;&middot;

context context = new initialcontext();

      // get the connection factory

      // create the connection

      // create the session

&middot;&middot;&middot;&middot;&middot;&middot;

// look up the destination

      topic = (topic)context.lookup(&quot;topic/mytestapptopic&quot;);

      // create a publisher

// publish the message

&middot;&middot;&middot;&middot;&middot;&middot;

5. JBossEJB in 3.0Use javamailService configuration.
Because JBossProvides javamailService implementation, so in JBossUse javamailIs very convenient. It needs to configure the mail. service file. This file is very simple and does not need to be described. The configuration can be completed smoothly according to the comments in the mail. service file.

Figure 5 uses javamailThe code snippet of A sessionbean of the service.

Figure 5 Use javamailSample Code

The above example briefly introduces JBossThe most basic configuration and deployment methods of Session Bean, Entity Bean, and Message Drive bean in 3.0.Basic EJBApplication development. In this articleSome complex configurations in applications, such as permission settings, resource management, and many configurations in cmp2.0, are not involved. I hope you can better master JBoss in constant practice and communication..

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.