Jboss+ant implementing an EJB stateless session bean instance

Source: Internet
Author: User
Tags jboss server

EJB is divided into session bean, entity Bean, Message-driven bean,session Bean and stateless session bean and stateful session bean.

The session Bean is responsible for interacting with the client, where the business logic is written, and the database can be manipulated directly through JDBC in the session bean, but in most cases the database operation is done through the entity bean.

Normally, we use the most stateless bean, because its bean instance is available to multiple users, so it has a higher performance than a stateful bean. Just because a bean instance is being used by multiple users. Then, the value set by the previous user may be modified by the latter user, so it cannot correctly save a user-set value because it is stateless. The following is a jboss+ant implementation of an EJB stateless session bean instance to help you get to know the EJB initially.

1. Create a plain Java project that references all the jar packages in the Jboss-4.2.3.ga\client directory when it is created;

2. Create a HelloWorld interface;

Package Cn.tgb.ejb3;public interface HelloWorld {public string SayHelloWorld (string name);}

3. Create an implementation class that implements the HelloWorld interface;

Package Cn.tgb.ejb3.impl;import Javax.ejb.remote;import Javax.ejb.stateless;import cn.tgb.ejb3.helloworld;@ Stateless@remote (helloworld.class) public class Helloworldbean implements HelloWorld {@Overridepublic String SayHelloWorld (String name) {return name + "say: Hello, world! ";}}
in the Helloworldbean class, we use the annotation stateless to indicate that the current class is stateless, annotated remote to indicate the current class of external access interface.
4. Create a new Build.xml file under the SRC directory of the project and use Ant to manage the project construction process;

<?xml version= "1.0" encoding= "UTF-8"? ><project name= "HelloWorld" basedir= "." ><property name= "Src.dir" value= "${basedir}/src"/><property name= "Build.dir" value= "${basedir}/build"/ ><property environment= "env"/><property name= "Jboss.home" value= "${env". Jboss_home} "/><property name=" jboss.server.config "value=" Default "/><path id=" Build.classpath "> <fileset dir= "${jboss.home}/client" ><include name= "*.jar"/></fileset></path><target Name= "Prepare" ><delete dir= "${build.dir}"/><mkdir dir= "${build.dir}"/></target><target Name= "Compile" depends= "prepare" description= "compile EJB project" ><javac srcdir= "${src.dir}" destdir= "${build.dir}" > <classpath refid= "Build.classpath"/></javac></target><target name= "Ejbjar" depends= "compile" Description= "EJB Engineering Package" ><jar jarfile= "${basedir}/${ant.project.name}.jar" ><fileset dir= "${build.dir}" ><include name= "**/*.class"/> </fileset></jar></target><target name= "Deploy" depends= "Ejbjar" description= "Publish EJB Project" > <copy file= "${basedir}/${ant.project.name}.jar" todir= "${jboss.home}/server/${jboss.server.config}/deploy"/ ></target><target name= "Undeploy" description= "Uninstalling EJB" ><delete file= "${jboss.home}/server/${ Jboss.server.config}/deploy/${ant.project.name}.jar "/></target></project>
5. In Eclipse's Outline window, select deploy, right-run as ant build, and after doing this, ant will be in the current project root directory and jboss-4.2.3.ga\server\default\ The Deploy directory generates the jar file for the project;

6. Start the JBoss server;

7. Invoke the method provided by the EJB stateless session bean in other projects through Jndi;

Package Com.tgb.test;import Java.util.properties;import Javax.naming.initialcontext;import Javax.naming.namingexception;import Cn.tgb.ejb3.helloworld;public class Ejbclient {public static void main (string[] args) {Properties props = new Properties ();p rops.setproperty ("Java.naming.factory.initial", " Org.jnp.interfaces.NamingContextFactory ");p rops.setproperty (" Java.naming.provider.url "," localhost:1099 "); Props.setproperty ("java.naming.factory.url.pkgs", "org.jboss.naming"); try {initialcontext ctx = new InitialContext ( props); HelloWorld HelloWorld = (HelloWorld) ctx.lookup ("Helloworldbean/remote"); System.out.println (Helloworld.sayhelloworld ("Beijing")); catch (Namingexception e) {System.out.println (E.getmessage ());}}}
to this, we completed the EJB stateless session bean instance creation process, hope to help everyone progress.

Jboss+ant implementing an EJB stateless session bean instance

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.