Eclipse+weblogic 12 develops a simple enterprise application

Source: Internet
Author: User

Learning about EJB content, encountered a lot of problems, browsing countless Java EE and WebLogic official documents, on the Google countless searches have no answer, maybe I want to find the answer too unpopular. It all started with the "cart" example in the official Java EE document, and I was puzzled by him for a whole day. Because this project in the Netbeans+glassfish environment can run very well, directly right click the project, point to run on it. The result came to eclipse and ran into a bunch of problems, either the injected EJB variable was null or the class could not be found. After a day of groping, now finally have a clue, now sorted out, hope can help everyone. And you may have a better way or have questions and comments about my narrative, please comment boldly. first, the application environment

This article assumes that you already have experience with the configuration of Eclipse or Spring Source tool and WebLogic. This project uses Application Client project to access the EJB module, which is more complex than a Web project managed directly with the Java EE container, because Web Client and EJB modules are in the same container and can be easily used dependency Injection (Dependency injection). The application client, which runs in a separate application client container, needs to interact with the Java EE container to get the resources in the container, requiring additional settings.

Tools used: Spring Source Tool (essentially eclipse, generic), WebLogic command Tool.

Source code: Github---Hello project Two, create a project
1, create a new enterprise application Project.

Right-click the package Explorer space and select New->other.


Then select Enterprise Application Client under Java EE



Click Next, fill in the basic information of the project, here I created a Hello project, the operating environment to choose WebLogic.



In this interface tick generate Application.xml deployment descriptor and click New module to create EJB and client.


Cancel Web module, where we do not create Web client and connector. Name the application client and EJB module, and then click Finish.



Eclipse generates three projects for us, helloclient-application client, HELLOEJB-EJB module, and a Hello is responsible for packaging these two modules into an ear for deployment to WebLogic.



2, coding the EJB module

The code structure of the EJB module is as follows:



Where the Hello.java under the Hello.ejb.interfaces package is a remote business interface, where the client running in a different Java Virtual machine can also access the services it provides, the code is as follows:

Package hello.ejb.interfaces;

Import Javax.ejb.Remote;

@Remote Public
interface Hello {public
	
	string SayHello (string name);

}


Hellobean is a stateless session bean. Of course stateful also can, after all, on this one application Client, its code is as follows:

Package HELLO.EJB;

Import javax.ejb.Stateless;

Import Hello.ejb.interfaces.Hello;

@Stateless public
class Hellobean implements Hello {

	@Override public
	string SayHello (string name) { Return
		"Hello:" + name;
	}


A very simple way to add "Hello:" to the name variable, and then return.

3, set application Client

Right-click Helloclient and select Properties.



Open Deployment aseembly menu, select Manifest Entries, click Add ..., add Helloejb.jar, add this package to compile without error. Dot finish.



The structure of the helloclient is as shown:



First look at the Hellotest.java code (I removed the default generated main Class):

Package com.hello.client;

Import Javax.ejb.EJB;

Import Hello.ejb.interfaces.Hello;

public class Hellotest {
	
	@EJB
	private static Hello hello;

	/**
	 * @param args */public
	static void Main (string[] args) {
		
		System.out.println (" David "));

	}


The hello variable uses a @ejb annotation to represent the variable using a container-dependent injection, and then calls its SayHello method in the main method and passes the variable "David". Then look at the manifest.mf file under Meta-inf, which reads:

manifest-version:1.0
Class-path:helloejb.jar
main-class:com.hello.client.hellotest
Class-path is added automatically when you select HELLOEJB Project dependencies, Main-class specifies the class that contains the Main method, which is required and will be known for a while when you run the project. Then there is a file that is important, weblogic-application-client.xml file, it is created, create a new file under Meta-inf, select Oracle WebLogic Application Client Descriptor:



Click Next, enter Name: Weblogic-application-client.xml.


Its contents are:

This file is specifically designed to describe the application client project, and to reference the EJB module, this file is required, specifying the name and class of the variable to reference the EJB module, and

The Jndi name of the EJB module, Java:global is used to find the Remote Business service interface. You can find the application name, EJB module name, and bean name from the WebLogic console.



4. Export Application client and EJB package

Since WebLogic's Oepe plugin cannot run application Client directly under Eclipse, we need to export it and its referenced EJB package to run at the command line. Right-click Helloclient,

Select Export:



Then choose the app Client Jar file under Java EE



Specifies the name and path of the jar package.


Click Finish to create, the same HELLOEJB, select EJB jar file under EJB, as with app client JAR file, all two JAR packages are placed under the same path.


5. Operation Helloclient

By deploying the Hello Enterprise application to WebLogic, HELLOEJB and helloclient are automatically deployed as well:


Then set the WebLogic environment variable, enter the WebLogic installation directory, the path below (according to your own installation path):


There are setwlsenv.cmd files that are used to temporarily set the environment variable, run it in cmd:


A successful your enviroment has been set is present. Now you can run our helloclient, and at the command line, enter the folder where you put Helloclient.jar and Helloejb.jar, and run the following command:

Java weblogic.j2eeclient.Main Helloclient.jar t3://localhost:7001, this is the WebLogic command-line tool, which is generally enough, You can also pass several parameters in the main method at most.


Hello:david, the call succeeded. If Weblogic-application-client.xml is not defined at the time, the following error occurs:


third, the end

With this project, I was more familiar with the WebLogic publishing Java EE Project, and also learned how to use the WebLogic T3 protocol to access its instances and to inject success into the EJB variable in helloclient. Do this project before there are a few test items, toss longer, still stay up late to check information, do not come out resolutely do not sleep, now finally can relax.

Related Article

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.