When you put jboss - 3.2. 6. Zip download, the next step is to unzip it, if you are up at windows, you can use winzip or winrar; If you are in linux, the unzip command used to my own as an example , assuming that I will extract it to the following directory

c: \ jboss - 3.2. 6

In relation to weblogic, websphere, such as j2ee servers, JBOSS startup are unexpectedly easy, if you are windows users, only need to enter the c: \ jboss - 3.2. 6 \ bin below, enter the run.bat command, JBOSS on running啦; are linux users, if so, only need to enter the c: \ jboss - 3.2. 6 \ bin below, enter the run.sh, then also run JBOSS. How to? Are pretty simple, right?
When you enter the run.bat or run.sh, you will find that the screen will continue to roll up some tips information, approximately 1 minute (dependent on the configuration of your machine, I was P4 1 .7 G, 128M), prompted information will stop rolling. (Note: If you are under the windows, then please let the DOS window has remained in this state, it can not be one of only!) Until its own stop.
This, JBOSS already is running the. And other J2EE server, like, JBOSS also provides a WEB console mode, use the method are in IE browser, type http:// 127.0.0.1:8080 / web-console /, see the welcome interface, it succeeded.
The preparation of the first EJB: "hello, world"

Now we start EJB programming. In the preparation of our first EJB, you should have a general EJB know, and if not, I suggest you first come online to find some of this article point of view, otherwise you will not be able to understand the following to describe the content.

Remote Interface

Remote interface means for the client can see in terms of the Call Interface

/ / HelloWorld.java
package sample;
/ ** / / * This is a remote interface, the client calls this interface to make a real ejb job * /
public interface HelloWorld extends javax.ejb.EJBObject
(
public String hello () throws java.rmi.RemoteException;
)

Home Interface

We can put as Home Interface EJB are a manufacturing factory, Home Interface EJB container told: "Hey, my client wants me to generate an EJB, I now put you in this mission to you!"

/ / HelloWorldHome.java
package sample;
/ ** / / * Home Interface EJB container how to tell generation or destruction of EJB instances * /
public interface HelloWorldHome extends javax.ejb.EJBHome
(
HelloWorld create () throws java.rmi.RemoteException, javax.ejb.CreateException;
)

EJB's implementation

Here is the real implementation of the EJB

/ / HelloWorldBean.java
package sample;
import javax.ejb.SessionContext;
/ ** / / * This type of concrete implementation of the remote interface HelloWorld * /
pubic class HelloWorldBean implements javax.ejb.SessionBean
(
private SessionContext ctx;
public void setSessionContext (SessionContext ctx)
(
this. ctx = ctx;
)
pubic void ejbRemove ()
(
System.out.println ( "ejbRemove ()");
)
public void ejbActivate ()
(
System.out.println ( "ejbActivate ()");
)
public void ejbPassivate ()
(
System.out.println ( "ejbPassivate ()");
)
/ ** / / * Hello method is the actual business logic, it can show the client "hello, world" in this string * /
public String hello ()
(
System.out.println ( "hello ()");
return "hello, world";
)
)

Well, this conversation all the EJB code completed, the next step we want to do is to prepare the deployment of its documents:

ejb - jar.xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<ejb-Jar>
<description> JBoss Hello World Application </ description>
<display-Name> Hello World EJB </ display - name>
<enterprise-Beans>
<session>
<ejb-Name> Hello </ ejb - name>
<home> Sample.HelloHome </ home>
<remote> Sample.Hello </ remote>
<ejb-Class> sample.HelloBean </ ejb - class>
<session-Type> Stateless </ session - type>
<transaction-Type> Bean </ transaction - type>
</ Session>
</ Enterprise - beans>
</ Ejb - jar>
Then we completed a session EJB's easy to prepare, but also provides a JBOSS extra configuration file: JBoss.xml, use it on JBOSS server more customized, but because this case is too easy , so we can not omit to write it.

Although we completed the preparation of this session EJB, but the last step remains to be done: package. First of all, we entered the current project's root directory:

cd F: \ project \ jboss - tutorial

Jar command will then implement all of the categories and ejb - jar.xml packing:

jar cf HelloWorld.jar sample META - INF

At this time you will find that many in the current directory under a document called HelloWorld.jar, this is our finally finished.

U.S. EJB deployment

EJB in JBOSS deployment is a very easy task, you simply will HelloWorld.jar copied to c: \ jboss - 3.2. 6 \ server \ default \ deploy directory it.

At this time, you can switch to the JBOSS running under the DOS window, you will find the new screen will appear the following message:

15: 09: 21, 184 INFO [MainDeployer] Starting deployment of
package: file: / F: / jboss
- 3.2. 3 / server / default / deploy / HelloWorld.jar
15: 09: 21, 324 INFO [EjbModule] Creating
15: 09: 21, 354 INFO [EjbModule] Deploying HelloWorld
15: 09: 21, 464 INFO [EjbModule] Created
15: 09: 21, 484 INFO [EjbModule] Starting
15: 09: 21, 555 INFO [EjbModule] Started
15: 09: 21, 555 INFO [MainDeployer] Successfully completed
deployment of package: file: / F: / jboss - 3.2. 6 / server / default / deploy / HelloWorld.jar

Client-side code

If there is no client-side code, then the EJB almost useless for us. Below we will prepare a client-side code to call the HelloWorld.

If you are on the same machine running the client code and JBOSS server, then no need to amend the following code can run, but your client is running on another machine, then you need to source the corresponding line change:

/ ** / / * The following is a client need to modify source code in the line * /
env.put (Context.PROVIDER_URL, "localhost: 1099");

EJB deployed in the assumption that one IP address is 192 Units. 168.0 .1 on the machine, then it should be more than source code to read as follows:

/ ** / / * The following is a client of the revised source line * /
env.put (Context.PROVIDER_URL, "192.168.0.1:1099");

/ ** / / * HelloWorldClient.java * /
package sample;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Hashtable;
public class HelloWorldClient
(
public static void main (String [] args)
(
Hashtable env = new Hashtable ();
env.put (Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put (Context.PROVIDER_URL, "localhost: 1099");
env.put ( "java.naming.factory.url.pkgs", "org.jboss.naming: org.jnp.interfaces");
try
(
Context ctx = new InitialContext (env);
Object obj = ctx.lookup ( "HelloWorld");
HelloWorldHome home = (HelloWorldHome) javax.rmi.PortableRemoteObject.narrow (
obj, HelloWorldHome. class);
HelloWorld helloWorld = home.create ();
System.out.println (helloWorld.hello ());
helloWorld.remove ();
)
catch (Exception e)
(
e.printStackTrace ();
System.out.println ( "Exception:" + e.getMessage ());
)
)
)

The six roles are:
Enterprise Bean Provider: EJB component developers;
1) EJB Component Developer (Enterprise Bean Provider)
EJB component developers responsible for developing rules to implement business logic EJB components, EJB components developed package ejb - jar file. EJB component developer is responsible for the definition of EJB's remote and home interface, the preparation of the implementation of business logic of the EJB class, providing the deployment of EJB deployment file (deployment descriptor).
EJB deployment file contains the names, EJB used in the allocation of resources, such as JDBC and so on. EJB component developers are typically experts in the field of commercial application development.
EJB component developers do not need proficiency in system-level programming, therefore, does not need to know the deal with a number of system-level details, such as services, synchronization, security, distributed computing and so on.
2) Application Portfolio From (Application Assembler)
Application of combination use of EJB is responsible for a complete portfolio of applications. Application portfolio are sometimes required to provide a number of related procedures, such as in an e-commerce system, application portfolio are required to provide JSP (Java Server Page) program. Application portfolio must have used EJB's home and remote interface, but it does not need to know the implementation of these interfaces.
3) the deployment of persons (Deployer)
Persons responsible for the deployment of ejb - jar file to deploy to the user's system environment. System environment that includes some type of EJB Server and EJB Container. Must ensure that all deployment of EJB components by developers in the deployment file a statement of resources available, for example, the deployment must be configured EJB database resources required.
4) EJB server provider EJB server provider systems are experts in the field, proficient in the distributed transaction management, distributed object management and other system-level services. EJB server provider in general by the operating system developers, middleware developers or database developers.
5) EJB container provider (EJB Container Provider)
EJB container provider to provide the following functions:
EJB deployment tool provides for the deployment of EJB components provide the best operating environment. EJB containers EJB is responsible for providing transaction management, security management and other services.
EJB container provider must be a system-level programming experts, but also has some experience in the field of application.
EJB container provider is mainly directed at the development of a scalable, with the transaction management functionality integrated into the EJB container server. EJB container provider for the EJB component developer provides a set of standard, easy-to-use API Access EJB container, EJB components so that developers do not need to know EJB server in a variety of technical details.
EJB container provider is responsible for providing system monitoring tools used to real-time monitoring of EJB containers and run on the EJB container component state.
6) System Administrator (System Administrator)
System administrator is responsible for the EJB server and container to provide an enterprise-class computing and network environments.
System administrator is responsible for the use of EJB servers and containers to provide the monitoring and management tools to monitor the operation of EJB components.