Myeclipse+jboss Create the first EJB, deploy and remotely invoke the entire process through Jndi

Source: Internet
Author: User
Tags jboss jboss server

In the EJB3 specification, entity beans are discarded, instead of entities and JPA (Java persistence Api,java data Persistence API) as a data persistence layer solution. Now look at the EJB3 specification:

Figure 1

About the connection between entity and JPA specification and hibernate this article is intended to study Sessionbean (Session Enterprise Bean), especially stateless (stateless) sessionbean.

The first step

Create a new EJB project in MyEclipse:

Figure 2

JPA support is temporarily not selected, MAVEN is not enabled, click Finish directly.

Create an EJB Sessionbean named HelloWorld:

Right click src--new Select EJB3 Sessionbean to see the following interface

Figure 3

Note that session type selects stateless (does not maintain client state), automatically creates interface remote (the system automatically creates the Helloworldremote interface for us, exposes the client to call our remote method), Click Finish.

@Statelesshelloworldremote {}

We see that the system has added annotations to our function names. This way of @annotation annotations replaces the way XML is configured in the EJB2 specification, but it is also compatible with the way XML is configured. There are several @annotation annotations on EJBS we'll discuss later.

In Helloworldremote we write a remote method HELLOEJB:

Import Javax.ejb.Remote; @Remotehelloworldremote {publicstring HELLOEJB (string name);}  

Control+s and then found HelloWorld The class name of the implementation class is marked up by the drop line, the mouse moves up, prompting us to have an interface method that is not implemented, point to the first option, MyEclipse directly help us create the template code for the method, which is also the first write interface ( when there are lots and lots of methods ) then write the quick way to create the implementation class.

Figure 4

The topic of this article is to create an EJB and reference it through Jndi, so in our implementation class we must bind to a Jndi name, where we bind Jndi directly through @stateless (mappedname= "HELLOEJB").

Package com.getcoo.service; import javax.ejb.Stateless; @Stateless (mappedname="HELLOEJB")helloworldremote {publicString HELLOEJB (String name) {// TODO auto-generated Method Stubreturn name+"Hello, Welcome to my EJB." ";  }}

Next we need to deploy this project to JBoss server, because I have a little problem with myeclipse deployment here, we use the most clumsy deployment method:

Right click on this item and select Export Project

Figure 5

Export as Jar file

Figure 6

Select the following export path and click OK.

Open the JBoss console again: localhost:8080

Figure 7

Expand the path of the diagram, we found that Helloejb.jar still did not come out, I again from the upper right corner of Add resource again (lazy to find reasons),

Figure 8

Click Continue,ok should be deployed.

Figure 9

Figure 10

Status becomes success.

Step Two:

Or do you want to create a test class under this project (if you build a common Java project, you need to copy the above two Java files, or the jar is copied to the past as LIB, otherwise it cannot import):

/** *  */Package com.getcoo.client;Import java.util.Properties;Import Javax.naming.Context;Import Javax.naming.InitialContext;Import javax.naming.NamingException;Import Com.getcoo.service.HelloWorldRemote;/** *@author 50% Cream * */PublicClasshelloejbtest { Publicvoid Test ()Throws namingexception{   Context jbossctx = Getjbossinitialcontext ();   Helloworldremote MYEJB = (helloworldremote) jbossctx.lookup ("HELLOEJB");   System.out.println ("A reference to the remote EJB instance was successfully obtained, and the reference instance was:" +MYEJB);   System.out.println (MYEJB.HELLOEJB ("Ink 50% Cream"));} Private Context Getjbossinitialcontext (){  Final String init_factory="Org.jnp.interfaces.NamingContextFactory";  Final String server_url="localhost:1099"; Context ctx=Null  try{  Properties props =New Properties ();  Props.put (context.initial_context_factory,init_factory);  Props.put (Context.provider_url, Server_url);  ctx=New InitialContext (props); }  catch (Namingexception ne) {   System.err.println ( "Cannot connect to JBoss Server in:" +server_ URL);    Ne.printstacktrace ();  }  return ctx;}   public static void main (string[] args) throws exception {   Helloejbtest client= new helloejbtest ();   Client.test ();   }}           /span>                

Run a bit:

Cannot connectJbossServer in:localhost: 1099Javax. naming. Noinitialcontextexception:CannotInstantiateClassOrg. JNP. interfaces. Namingcontextfactory[Root exception is java.lang.ClassNotFoundException:org.jnp.interfaces.NamingContextFactory] AtJavax. naming. SPI. Namingmanager. Getinitialcontext (Namingmanager. java: 657) AtJavax. naming. InitialContext. GETDEFAULTINITCTX (InitialContext. java: 288) AtJavax. naming. InitialContext. Init (InitialContext. java: 223) AtJavax. naming. initialcontext.<Init> (InitialContext. java: 197) AtCom. Getcoo. Client. Helloejbtest. Getjbossinitialcontext (Helloejbtest. java: 38) AtCom. Getcoo. Client. Helloejbtest. Test (Helloejbtest. java: 22) AtCom. Getcoo. Client. Helloejbtest. Main (Helloejbtest. java: 50)CausedByJava. lang. ClassNotFoundException:Org. JNP. interfaces. Namingcontextfactory AtJava. Net. Urlclassloader$1. Run (URLClassLoader. java: 200) AtJava. Security. Accesscontroller. doprivileged (NativeMethod) AtJava. Net. URLClassLoader. Findclass (URLClassLoader. java: 188) AtJava. lang. ClassLoader. LoadClass (ClassLoader. java: 307) AtSun. misc. launcher$Appclassloader. LoadClass (Launcher. java: 301) AtJava. lang. ClassLoader. LoadClass (ClassLoader. java: 252) AtJava. lang. ClassLoader. Loadclassinternal (ClassLoader. java: 320) AtJava. lang. Class. FORNAME0 (NativeMethod) AtJava. lang. Class. forname (Class. java: 247) AtCom. Sun. naming. Internal. VersionHelper12. LoadClass (VersionHelper12. java: 46) AtJavax. naming. SPI. Namingmanager. Getinitialcontext (Namingmanager. java:654)  ... 6 moreexception in thread " main "java.lang at com.getcoo< Span class= "class" >.client.test (helloejbtest.java :23)  at com.getcoo.main (helloejbtest.java :50)               

Why? I examined it carefully, I am not wrong here, if it is a lot of mistakes to frighten the right to find the wrong words, not a good programmer, what do we want to see? To find root exception, the root cause is abnormal, often this is the problem key.

Root Exception Displays:

Class:org.jnp.interfaces.NamingContextFactory is    java.lang.ClassNotFoundException:            Org.jnp.interfaces.NamingContextFactory]

How clear the anomaly, org.jnp.interfaces.NamingContextFactory this kind of system did not find. So I'm telling you that we quoted this class, but we're not adding the relevant lib.

This class is the Jndi naming context factory class for JBoss, so let's just add the JBoss lib that we added in the previous example:

Figure 11


Run one More time:

A reference to the remote EJB instance was successfully obtained, and the reference instance is: Proxy to jboss.j2ee:jar=helloejb.jar,name=helloworld,service=ejb3 implementing [   com.getcoo.  Service. Helloworldremote] Ink 50% cream, hello, welcome to my EJB. 

Congratulations, here, you are welcome to step into the world of EJB.

Myeclipse+jboss Create the first EJB, deploy and remotely invoke the entire process through Jndi

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.