EJB improves EJB application development efficiency, stateless development of native interface beans, development state beans through ant

Source: Internet
Author: User
Tags object serialization jboss

The JBoss integration into eclipse

Turn off JBoss Console news Ctrl + C, and Myeclipse→servers→jboss can configure JBoss.


Improve the development efficiency of EJB applications with Ant

In HelloWorld add ant, right click on Project New Xml:build.xml, right click Open Mode select Ant.

Compile, package, publish, and publish accordingly

<?

XML version= "1.0" encoding= "UTF-8"?

><!--The path to the Basedirz project, which represents the path--><project name= HelloWorld "basedir=" in Build.xml. ><property name= "Src.dir" value= "${basedir}\src" ></property><!--refers to system variables--><property Environment= "env" ></property><property name= "Jboss.home" value= "${env". Jboss_home} "></property><!--used to specify the configuration items that are currently used by JBoss--><property name=" Jboss.server.config "value=" Default ></property><!--folder--><property name= "Build.dir" value= "${basedir}\ for compiling the source file class class Build ></property><!--EJB-dependent jar file--><path id= "Build.classpath" ><fileset dir= "${ Jboss.home}\client "><include name=" *.jar "/></fileset><pathelement location=" ${build.dir} "/> </path><target name= "Prepare" ><delete dir= "${build.dir}" ></delete><mkdir dir= "${ Build.dir} "></mkdir></target><target name=" compile "depends=" prepare "description=" compile "><! --Compile the source file. Destdir compiled class storage folder--><javac srcdir= "${src.dir}"Destdir=" ${build.dir} "><!--compiling dependent jar files--><classpath refid=" Build.classpath "></classpath> </javac></target><target name= "Ejbjar" depends= "compile" description= "Create EJB Advertisement Package" ><!--${ Ant.project.name} is the above name Helloworld--><jar jarfile= "${basedir}\${ant.project.name}.jar" ><fileset Dir= "${build.dir}" ><include name= "**/*.class"/></fileset></jar></target><target Name= "Deploy" depends= "Ejbjar" description= "Advertise EJB" ><copy file= "${basedir}\${ant.project.name}.jar" todir= "${ Jboss.home}\server\${jboss.server.config}\deploy "></copy></target><target name=" Undeploy " description= "Uninstalling EJB" ><delete file= "${jboss.home}\server\${jboss.server.config}\deploy\${ant.project.name}. Jar "></delete></target></project>


Announcement: Start JBoss in the Servers view. Then right-click Deploy→run as→ant in the Build.xml outline view ...

(Find coding problem my project is GBK, change the project into UTF-8 can);

To unload the EJB, you should stop JBoss before running Undeploy.

Developing a stateless bean with a local interface

We have previously introduced the remote interface. The process of invoking an EJB through a remote interface. First, the client needs to establish a socket communication with the EJB. They need to send IIOP protocol information back and forth on the communication pipeline, and the Java object that holds the data must be serialized because the data is to be transmitted over the network. In this process. There is the overhead of network communication, the cost of protocol parsing, the overhead of object serialization, and so on. Because EJB is a distributed technology. It agrees that the client and EJB applications are on different machines. So these performance costs are also certain.

In real-world development, however, client and EJB applications execute in the same jboss.

At this time, client access to the EJB is it necessary to go above the network communication it? As far as we know, when the client and EJB are in the same JVM, they are completely able to interact with memory, thus avoiding the performance overhead of network traffic. Now that we have all this in mind, the EJB Expert Group also thought of this and introduced a local interface . The EJB is called through the local interface and interacts directly in memory. This avoids the various performance overhead caused by network communication. But a little. It is important to note that only the client and the EJB application execute within the same JVM, we can invoke the local interface, otherwise we just call the remote interface.

Only the client and EJB are advertised within the same jboss. We think that they are in the same JVM.

Client and EJB deployed to the same machine

Specify Helloworldbean as the local interface (@Remote change to @local):

@ Stateless

@Local (HelloWorld. class)

Public class Helloworldbean implementsHelloWorld {

The stateless bean for the local interface has been developed.

Deploy to EJBs with ant deploy.

Create a new Web Project as the client for the EJB. and deployed to JBoss, so that the client and EJB are in the same JVM.

Create a new web projcet:ejbclient and deploy to JBoss.

The EJB is invoked with a JSP. Create a new test.jsp, change the encoding format to utf-8 so that you can access the EJB through the local interface (refer to the HelloWorld project: Right-click the project build path→conf ... →projects→add):

<%@ page language="java"import="java.util.*"pageencoding="UTF-8" %>

<%@ page Import="javax.naming.*,cn.hqu.ejb3.*"%>

<%

String path = Request.getcontextpath ();

String basepath = request.getscheme () +"://"+request.getservername () +":"+request.getserverport () +path+"/";

%>

<! DOCTYPE HTML Public "-//w3c//dtdhtml 4.01 transitional//en" >

< HTML >

< Head >

< Base href="<%=basepath%>">

< title > My JSP ' test.jsp ' starting page</title>

< Meta http-equiv="Pragma"content="No-cache">

< Meta http-equiv="Cache-control"content="No-cache">

< Meta http-equiv="Expires"content="0">

< Meta http-equiv="keywords"content="Keyword1,keyword2,keyword3">

< Meta http-equiv="description"content="This is my page">

<!--

<linkrel= "stylesheet" type= "text/css"href= "Styles.css" >

-

</ Head >

< Body >

<%

Try {

Initialcontextctx = new initialcontext ();

Helloworldhelloworld = (HelloWorld) ctx

. Lookup ("helloworldbean/local");

Out.println (Helloworld.sayhello (" local su zhi da "));

}catch (Namingexception e) {

E.printstacktrace ();

}

%>

</ Body >

</ HTML >

The client application has been developed. Export the war file, copy the war file to the release folder of JBoss, and see the console discover that the app is published successfully (can deploy the project to JBoss via MyEclipse, start JBoss to be published)

http://localhost:8080/EJBClient/test.jsp

Invoking the EJB through the local interface succeeded. Web applications and EJBS are deployed to the same jboss. In the same JVM. Be able to interview.

Client and EJB are deployed to different machines.

Create a new local interface and create a new helloworldlocal interface in the HelloWorld project. Inherit the remote interface HelloWorld.

The Helloworldbean implements the Helloworldlocal and HelloWorld interfaces.

@Stateless

@Remote (HelloWorld. class)

@Local (helloworldlocal. class)

Public class Helloworldbean implements helloworld,helloworldlocal {

@Override

Public Stringsayhello (String name) {

return name+" said: Hello!

";

}

}

When deploying on the same machine, priority calls are local, and on different machines we can only access through the remote interface.

Local invocation: Deployment launches JBoss execution http://localhost:8080/EJBClient/test.jsp

Remote invocation:

Change the HelloWorld ejbclient class to a remote interface:

Helloworldhelloworld = (HelloWorld) ctx.lookup ("Helloworldbean/remote");

Deployment announcement: Perform ant deploy and start JBoss execution Main method.

When the client invokes the EJB through the remote interface, now they are in different JVMs, only through the remote interface, to execute the Ejbclient Mian method. A successful interview.

In practical applications, we'd better implement both local and remote excuses. The main solution is to be able to publish in the same JBoss or different jboss can be used.

Developing a stateful bean

Only need to change @stateless to @stateful can be labeled as stateful bean

@Stateful

@ Remote (HelloWorld. class)

@Local (helloworldlocal. class)

Public class Helloworldbean implementsHelloWorld, helloworldlocal {

@Override

Public Stringsayhello (String name) {

return name+" said: Hello. ";

}

}

Ant Deploy Deployment

Stateful and stateless have little difference in code. However, it is not the same as the technology used to manage beans.

Two kinds of management techniques for bean instances

1. Stateless beans using instance pooling technology to manage beans

2. Stateful bean using activation (activation) to manage beans

Each user has its own bean instance, which is equivalent to how many instances of the bean are served by the number of users. Assuming that no matter what mechanism, this will inevitably lead to serious consumption of server resources, and finally a down-machine strike.

In order to solve the problem. EJB introduced the activation machine system. The mechanism of the principle to achieve this is, in ejbserver if necessary, conserve resources. Example of recovering a bean from memory. The session state that will hold it is serialized to disk, and they consume memory freed. If at this point the client properly EJB initiates the request again, the EJB instantiates a container bean example again and resumes from the hard disk's state in the previous.

Code: Http://pan.baidu.com/s/1gdGIi8V

EJB improves EJB application development efficiency, stateless development of native interface beans, development state beans through ant

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.