Ejb stateless bean

Source: Internet
Author: User

In this article, we will compile a simple stateless SessionBean. When publishing an EJB program, you generally need to publish the EJB program in the form of a jar file. These jar files will be placed in the <JBoss installation directory> \ server \ default \ deploy directory. If you develop an EJB program in Eclipse, You need to perform some configuration. In the Preferences dialog box, set the JBoss installation directory (for example, D: \ jboss5 ). Then, add the jboss running item in the running configuration dialog box. In jboss4.2 and later versions, by default, jboss only receives requests from localhost or 127.0.0.1, that is, only local access is received. To enable jboss to receive requests from other addresses, you must use the-B command line parameter when starting jboss. The following startup command is used:

Run. bat-B 200.200.200.123
Run. bat-B 0.0.0.0

The first line of the command above indicates that jboss can receive requests from 200.200.200.123. The second command indicates that jboss can receive requests from any address. If the-B parameter is not set, JBoss throws the following exception when accessing EJB in a non-local mode:

Exception in thread "main" javax. naming. communicationException: cocould not obtain connection to any of these urls: 192.168.17.105: 1099 and discovery failed with error: javax. naming. communicationException: Receive timed out [Root exception is java.net. socketTimeoutException:
Receive timed out] [Root exception is javax. naming. communicationException: Failed to connect to server/192.168.17.105: 1099 [Root exception is javax. naming. serviceUnavailableException: Failed to connect to server/192.168.17.105: 1099 [Root exception is java.net. connectException:
Connection refused: connect]
At org. jnp. interfaces. NamingContext. checkRef (NamingContext. java: 1725)
At org. jnp. interfaces. NamingContext. lookup (NamingContext. java: 689)
At org. jnp. interfaces. NamingContext. lookup (NamingContext. java: 682)
At javax. naming. InitialContext. lookup (InitialContext. java: 392)
At test. Client. main (Client. java: 29)
......

1. Configure the development environment

For Development in eclipse, you also need to set the-B command line parameter. In the run configuration dialog box, set the-B command line parameter as shown in Figure 1.

Figure 1

2. Develop stateless Session Bean

To develop an EJB program in Eclipse, you must first create an EJB Project named MyEJB ). Create an interface (remote interface) and a Session Bean. The remote interface code is as follows:

Package service;

Import java. util. List;
Import javax. ejb. Remote;
Import entity. Greeting;

@ Remote
Public interface Greeter
{
Public String greet (String message );
Public List <Greeting> getAllGreetings ();
}

In the above Code, @ remote annotation is used to define the greeter interface as a remote interface, that is, the local session bean can be accessed on other client machines through this interface. A greeting class is also used in the greeter interface. The code for this class is as follows: Package entity;

Import java. Io. serializable;

Public class greeting implements serializable
{
Private int ID;
Private string name;
Public int GETID ()
{
Return ID;
}
Public void setid (int id)
{
This. ID = ID;
}
Public String getname ()
{
Return name;
}
Public void setname (string name)
{
This. Name = Name;
}
}

Note that because the greeting class will be transmitted to the client on the instance, the class must implement the java. Io. serializable interface.
The code for compiling the session bean is as follows: Package Service;

Import java. util. arraylist;
Import java. util. List;
Import javax. EJB. stateless;
Import entity. greeting;

@ Stateless
Public class greeterbean implements greeter
{
@ Override
Public List <Greeting> getAllGreetings ()
{

List <Greeting> greetings = new ArrayList <Greeting> ();
Greeting greeting = new Greeting ();
Greeting. setId (12 );
Greeting. setName ("bill gates ");
Greetings. add (greeting );
Greeting = new Greeting ();
Greeting. setId (334 );
Greeting. setName ("Li Ning ");
Greetings. add (greeting );
Return greetings;
}

@ Override
Public String greet (String message)
{
Return "hello" + message;
}

}

In the above Code, @ Stateless annotation is used to define the GreeterBean class as a Stateless Session Bean. If JBoss is in the startup status and saves the classes and interfaces written above, Eclipse automatically compiles the above Code, generates a jar file, and publishes it to the deploy directory of jboss. The directory structure of the jar file is as follows:

MyEJB. jar
Entity \ Greeting. class
Service \ Greeter. class
Service \ GreeterBean. class
META-INF \ MANIFEST. MF
META-INF \ jboss. xml

The two files in the META-INF directory are automatically generated by Eclipse when an EJB project is created, and we don't have to worry about it. You can also manually compile the above interfaces and classes and use the jar command to generate a jar file.

3. Write client programs

This article uses a remote interface to access the Session Bean. Therefore, you must specify the IP addresses of all EJB machines during access. Package test;

Import java. util. Properties;
Import javax. naming. Context;
Import javax. naming. InitialContext;
Import service. Greeter;

Public class Client
{

Public static void main (String [] args) throws Exception
{
Properties prop = new Properties ();

// Set relevant property values

Prop. setProperty (Context. PROVIDER_URL, "192.168.17.105: 1099 ");
Prop. setProperty (Context. INITIAL_CONTEXT_FACTORY, "org. jnp. interfaces. NamingContextFactory ");
InitialContext ctx = new InitialContext (prop );

// Start to call the Greeter Interface

Greeter greeter = (Greeter) ctx. lookup ("GreeterBean/remote ");
System. out. println (greeter. greet ("Li Ning "));
System. out. println (greeter. getAllGreetings (). get (0). getName ());
}
}

In the above Code, context. provider_url is used to set the IP address and port number of the server.
The running result of the above area code is as follows:

Hello, Li Ning
Bill gates

In addition to setting the property value in the program, you can also set it through the JNDI. properties file. This file should be placed in the src directory of the eclipse project. The content of this file is as follows:

Java. naming. factory. initial = org. jnp. interfaces. NamingContextFactory
Java. naming. provider. url = 192.168.17.105: 1099

If you use the JNDI. properties file, you do not need to set the corresponding property value in the client program. Therefore, you can use the following code to call the Session Bean:
Package test;

Import javax. Naming. context;
Import javax. Naming. initialcontext;
Import service. greeter;

Public class client
{

Public static void main (string [] ARGs) throws exception
{
// You do not need to set the corresponding property value in the program
Initialcontext CTX = new initialcontext ();
// Start to call the Greeter Interface
Greeter greeter = (Greeter) ctx. lookup ("GreeterBean/remote ");
System. out. println (greeter. greet ("Li Ning "));
System. out. println (greeter. getAllGreetings (). get (0). getName ());
}
}

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.