Duggle EJB (1)

xiaoxiao2021-03-06  87

Although EJB 3.0 has been quarrel, it is not known as the EJB 3.0 foundation, I don't know when it is officially released, and the convenience of EJB 3.0 is not in time, when we have it, it is just trouble EJB 2.0.

Finally found a reason to use EJB slightly suitable. The system must support the multi-machine, where the most difficult solution is shared, so I think of EJB and started fighting with EJB.

According to J2EE TUTORIAL, EJB running with a Hello level in J2EE RI is not difficult. Here is my Hello EJB client code: context initial = new initialContext (); context myenv = (context) Initial.lookup ("java: comp / env"); object objref = myenv.lookup ("ejb / helloworld" ); HelloWorldHome home = (HelloWorldHome) PortableRemoteObject.narrow (objref, HelloWorldHome.class); HelloWorld helloEJB = home.create (); System.out.println (helloEJB.hello ( "dreamhead"));

Hello's implementation is very simple: public string hello (string name) {return "Hello," Name;}

According to this announcement, start the J2EE RI, run the client code: set appcpath = helloworldappclient.jar Runclient -client helloworldapp.ear -name HelloWorldClient -TextAuth

Bingo! success.

This is certainly not the story of the story, otherwise it is too sorry to "brim over" topics. The way we are most used to run Java programs is Java classname and running this code is actually a tool that comes with Runclient, J2EE RI, which is clear that this does not meet our daily behavioral specification. My goal, run the EJB client directly with Java.

Running directly HelloWorldClient is obviously not good, if you don't believe in evil, you can taste the taste of failure.

Some information gave me a little prompt, setting some necessary properties will help you can help me complete the task. So I added this sentence in the code: Properties Env = new Properties (); env.Put (context.initial_context_factory, "com.sun.enterprise.naming.serialinitcontextFactory); env.put (Context.Provider_URL," IIOP : //xxx.xx.xxx.xxx: 1050 "); Here is the main Context.initial_Context_Factory and Context.Provider_URL, which is actually two strings, representing" java.naming.factory.initial "and" Java.naming.Provider.URL, in some information, they often show people. The above code gives the settings for J2EE RI. Different application server settings are different. If you want to use similar code on your application server, you must first find what these two parameters on your application server. Because there are too little data about J2EE RI, I also hurt me for a long time. It's easy to find the parameters, write a bunch of code, don't forget to make it work! Context Initial = New InitialContext (ENV); there is a place to be changed, that is, the name of JNDI. Follow J2EE Tutorial all the way, we rarely in the "Java: Comp / ENV / EJB / HELLOWORLD" used to find it in code, is not the name of JNDI, but is just a reference name. Directly run the code, given just an error prompt. Remove it back to the JNDI name, the problem is solved. Object objref = initial.lookup ("myhelloworld");

J2EE's thing is not visible everywhere, so you have to run it yourself, take J2EE.jar under J2EE RI's lib directory, will save a lot of trouble. In addition, use this thing to run some attribute files, I use the most direct way to move things directly under the config directory.

The code can be compiled, don't worry. If you have to hear EJB, you must know something such as Stub, less, code is absolutely running, so we have to join HelloWorldAppClient.jar to classpath, the legendary Stub is here.

I think many people like me, run this example, put Server and Client on the same machine, the above IP is set to LocalHost or this IP. In this way, we can happiness through the first level and see the EJB's kind greeting.

Wanli Long March finally got out of the first step!

转载请注明原文地址:https://www.9cbs.com/read-108081.html

New Post(0)