「JBoss」EJB をさわってみる ( スタンドアロンアプリから利用 )

EJB をさわってみたので、その時のメモ。
EJB コンテナには JBoss を使うことにしました。

[ 環境情報 ]
Windows 7 SP1
JBoss Application Server 7.1.1.Final
Java SE 7 Update 45

内容的には以下のドキュメントを参考にやってます。

EJB invocations from a remote client using JNDI - JBoss AS 7.1 - Project Documentation Editor
https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI


1. EJB の作成
■ RemoteTest.java

package test;

public interface RemoteTest {
	String hello(String name);
	int add(int a, int b);
}

■ TestBean.java

package test;

import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless
@Remote(RemoteTest.class)
public class TestBean implements RemoteTest {
	@Override
	public String hello(String name) {
		return "Hello " + name;
	}

	@Override
	public int add(int a, int b) {
		return a + b;
	}
}

Eclipse 使ってる場合は [ Export ] - [ EJB JAR file ] で .jar ファイルを作成できるので楽です。Eclipse 使ってない場合は、以下のコマンドでコンパイル、.jar ファイル ( EJBTest.jar ) を作成します。

javac -cp %JBOSS_HOME%\modules\javax\ejb\api\main\jboss-ejb-_spec-1.0.1.Final.jar test\*.java
jar cvf EJBTest.jar META-INF test

※ .jar ファイルの中身は以下の感じです ( ソースコードも含んじゃってますが、気にせずで )。

jar tf EJBTest.jar
META-INF/
META-INF/MANIFEST.MF
META-INF/ejb-jar.xml
test/
test/RemoteTest.class
test/RemoteTest.java
test/TestBean.class
test/TestBean.java

■ MANIFEST.MF

Manifest-Version: 1.0
Created-By: 1.7.0_45 (Oracle Corporation)

ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.1" xmlns="http://java.sun.com/xml/ns/javaee" 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/ejb-jar_3_1.xsd">
  <display-name>EJBTest </display-name> 
 </ejb-jar>

作成した EJBTest.jar を %JBOSS_HOME%\standalone\deployments に配置して、JBoss 起動しておきます。

%JBOSS_HOME%\bin\standalone.bat


2. EJB クライアントの作成
■ EJBClient.java

package test;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;

public class EJBClient {
	private static final String JNDI_NAME = "ejb:/EJBTest//TestBean!test.RemoteTest";

	public static void main(String[] args) throws Exception {
		final Hashtable jndiProperties = new Hashtable();
		jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
		final Context context = new InitialContext(jndiProperties);
		RemoteTest test = (RemoteTest) context.lookup(JNDI_NAME);
		System.out.println(test.hello("hoge"));
		System.out.println(test.add(1, 2));
	}
}

※ 1 で作成した "test.RemoteTest" が見えるようにしてやってくださいね。

jboss-ejb-client.properties ( クラスパス上に配置する )

endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
 
remote.connections=default
 
remote.connection.default.host=127.0.0.1
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

あと、%JBOSS_HOME%\bin\client\jboss-client.jar にクラスパスを通しておく。


上記で作業完了です。EJBClient.java を実行すると、以下の結果が得られると思います。

Hello hoge
3


以上です。