Eclipse+Weblogic 12 develops a simple Enterprise Application

When I learned about EJB, I encountered a lot of problems, read the official documents of Java EE and Weblogic countless times, and searched countless times on google, but there was no answer. Maybe the answer I was looking for was too unpopular. This all originated from the "cart" example in the official Java EE documentation, and I was confused by him for a whole day. Because this project can run very well in the Netbeans+GlassFish environment, just right-click the project and click Run. As a result, I got Eclipse and encountered a bunch of problems, either the injected EJB variable was Null or the class could not be found. After a day of groping, I finally have a clue, and now I have sorted it out, I hope it can help everyone. And you may have a better way or have questions and comments about my narrative, please comment boldly.laughing out loud

1. Application environment

This article assumes that you already have configuration experience with Eclipse or Spring Source Tool and Weblogic. This project uses the Application Client project to access the EJB module, which is more complicated than the Web project managed directly by the Java EE container, because the web client and the EJB module are in the same container, so Dependency Injection can be easily used. The Application Client runs in a separate Application Client Container. It needs to interact with the Java EE container to obtain resources in the container, which requires some additional settings.

Tools used: Spring Source Tool (essentially Eclipse, common), weblogic command tool.

Source code: Github --- Hello project

2. Create a project


1. Create a new Enterprise Application Project.

Right-click on an empty space in the Package Explorer and select New->Other.


Then under Java EE select Enterprise Application Client



Click Next, fill in the basic information of the project, here I create a new hello project, and choose weblogic for the running environment.



Check Generate application.xml deployment descriptor in this interface, then click New Module to create EJB and Client.


Cancel the Web module, here we do not create Web Client and Connector. Name the Application client and EJB modules, then click finish.



Eclipse generates the following three projects for us, helloClient-application client, helloEJB-EJB module, and a hello is responsible for packaging these two modules into EAR and deploying them to Weblogic.



2. Write EJB module code

The code structure of the EJB module is as follows:



Among them, Hello.java under the hello.ejb.interfaces package is a remote business interface, that is, clients running in different Java virtual machines can also access the services provided by it. 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 is also possible. After all, this is the Application Client, and 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 method, add "Hello:" to the name variable, and return.

3. Set Application Client

Right-click on helloClient and select properties.



Open the Deployment Aseembly menu, select Manifest Entries, click Add..., and add helloEJB.jar. This package is added to compile without errors. Click Finish.



The structure of helloClient is shown in the figure:



First look at the code of HelloTest.java (I deleted 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(hello.sayHello("David"));

	}

}
The hello variable is annotated with @EJB to indicate that this variable uses container dependency injection, and then its sayHello method is called in the main method, passing the variable "David". Look at the MANIFEST.MF file under META-INF, its content is:

Manifest-Version: 1.0
Class-Path: helloEJB.jar
Main-Class: com.hello.client.HelloTest
Class-Path is automatically added when selecting helloEJB project dependencies. Main-Class is the class that specifies the main method. This is required, and you will know when you run the project later. Then there is one more important file, the weblogic-application-client.xml file, which is created like this. Create a new file under META-INF and select Oracle WebLogic Application Client Descriptor:



Click Next, enter the name: weblogic-application-client.xml.


Its content is:

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-application-client xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-application-client"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/javaee_5.xsd
	http://xmlns.oracle.com/weblogic/weblogic-application-client
	http://xmlns.oracle.com/weblogic/weblogic-application-client/1.3/weblogic-application-client.xsd">
	
	<!-- Define the JNDI name of the EJB module to be referenced -->
	<wls:ejb-reference-description>
		<!-- 要使用EJB的变量 -->
		<wls:ejb-ref-name>com.hello.client.HelloTest/hello</wls:ejb-ref-name>
		
		<!-- JNDI命名规则:java:global/[应用程序名]EJB模块名/Bean名 -->
		<wls:jndi-name>java:global/hello/helloEJB/HelloBean</wls:jndi-name>
	</wls:ejb-reference-description>

</wls:weblogic-application-client>
这个文件是专门用来描述Application Client项目的,若要引用EJB模块,这个文件是必须的,它指定了要引用ejb模块的变量的名称和所在的类,以及

EJB模块的JNDI名称,java:global是用来查找remote业务服务接口的。可以从weblogic console里面找到应用程序名,EJB模块名和Bean名。



4、导出Application Client与EJB包

由于WebLogic的OEPE插件不能直接在Eclipse下运行Application Client,所以我们需要把它和它引用的EJB包导出来,在命令行运行。右击helloClient,

选择Export:



然后选择Java EE下的App Client Jar file



指定Jar包的名称和路径。


点Finish创建,同样的helloEJB,选择EJB下的EJB JAR file,过程与App client jar file一样,这两个jar包都要放在同一个路径下。


5、运行helloClient

把hello这个Enterprise Application部署到WebLogic下,helloEJB和helloClient也会自动部署:


Then first set the WebLogic environment variables, enter the WebLogic installation directory, the path is as follows (according to your own installation path):


There is a setWLSEnv.cmd file in it, which is used to temporarily set environment variables. Run it in cmd:


When Your enviroment has been set appears, the setting is successful. Now we can run our helloClient, enter the folder where you put helloClient.jar and helloEJB.jar on the command line, and run the following command:

java weblogic.j2eeclient.Main helloClient.jar t3://localhost:7001, this is the command line tool of weblogic, generally this is enough, and at most you can pass several parameters in the main method later.


Hello: David appears, and the call is successful. If weblogic-application-client.xml is not defined at that time, the following error will occur:


3. End

Through this project, I became more familiar with the Java EE project released by weblogic, and also learned how to use WebLogic's t3 protocol to access its instance, and to inject the EJB variable hello in helloClient successfully. Before doing this project, there were still several test projects, and the tossing time was longer, and I stayed up all night to check the information.laughing out loud

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325938429&siteId=291194637