Fork me on GitHub

Programming Design Notes

GlassFish V3 Remote EJB

| Comments

首先制作 EJB 部份。制作一個 MessageRemote 的介面,然後再實作這個介面。
** EJB 3.1 不再需要 Local 的介面 **

MessageRemote:
package com.blogspot.lawpronotes.ejb;
import javax.ejb.Remote;

@Remote
public interface MessageRemote {
public String sayHello();
}

Message:
package com.blogspot.lawpronotes.ejb;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
* Session Bean implementation class Message
*/
@LocalBean
@Stateless
@EJB(name="java:global/HelloWorldEJB/Message", beanInterface=MessageRemote.class)
public class Message implements MessageRemote {

/**
* Default constructor.
*/
public Message() {
// TODO Auto-generated constructor stub
}

@Override
public String sayHello() {
// TODO Auto-generated method stub
return "Hello World";
}

}

現在可以將 EJB Project 放到 GlassFish 上,然後制作一個 JAR 並將 EJB 的 Remote 介面放到 JAR 內,緊記不要連同實作的 Class 放到 JAR 內,不然會發生錯誤。

現在制作 Servlet 部份。將剛才制作好的 JAR 放到 WEB-INF/lib 內。
新增一個 IndexServlet ,複製以下程式碼並貼上。
package com.blogspot.lawpronotes.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blogspot.lawpronotes.ejb.MessageRemote;

/**
* Servlet implementation class IndexServlet
*/
@WebServlet(name = "Index", urlPatterns = { "/index" })
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Properties properties;

/**
* @see HttpServlet#HttpServlet()
*/
public IndexServlet() {
super();
// TODO Auto-generated constructor stub
properties = new Properties();
properties.put("java.naming.factory.initial",
"com.sun.enterprise.naming.impl.SerialInitContextFactory");
properties.put("java.naming.factory.url.pkgs",
"com.sun.enterprise.naming");
properties.put("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
properties.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
properties.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
}

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
try {
InitialContext context = new InitialContext(properties);
Object obj = context.lookup("java:global/HelloWorldEJB/Message");
MessageRemote message = (MessageRemote) PortableRemoteObject
.narrow(obj, MessageRemote.class);
out.print(message.sayHello());
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.print(e.toString());
}
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}

web.xml 設定如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>HelloWorldWAR</display-name>
<welcome-file-list>
<welcome-file>index</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

現在將 WAR Project 放上 GlassFish。再打開 IndexServlet 應該會出現 Hello World 字樣。

單機 Java 應用程式也是差不多的做法,但一有點要注意的是要將 GlassFish 的 appserv-rt.jar 加到單機 Java 應用程式下,才能正確地用到 Remote EJB。當然 EJB 的 JAR 也是必需的。
** appserv-rt.jar 在 GlassFish 的 lib 資料夾內 **

Main:
package com.blogspot.lawpronotes.app;

import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import com.blogspot.lawpronotes.ejb.MessageRemote;

public class Main {
public static void main(String[] args) throws NamingException{
Properties properties = new Properties();
properties.put("java.naming.factory.initial", "com.sun.enterprise.naming.impl.SerialInitContextFactory");
properties.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
properties.put("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
properties.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
properties.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext context = new InitialContext(properties);
Object obj = context.lookup("java:global/HelloWorldEJB/Message");
MessageRemote message = (MessageRemote)PortableRemoteObject.narrow(obj, MessageRemote.class);
System.out.println(message.sayHello());
}
}

在 Console 會看見 Hello World 字樣。

Eclipse 範例在這裡下載: RemoteEJB.zip

相關書籍: Beginning Java™ EE 6 Platform with GlassFish™ 3: From Novice to ProfessionalEnterprise JavaBeans 3.1Java EE 6 Tutorial, Volume I, The (4th Edition) (Java Series)