5.2. リモート Jakarta Enterprise Beans クライアントの使用
5.2.1. 初期コンテキストルックアップ
初期コンテキストの作成時に PROVIDER_URL プロパティーを使用してリモートサーバーのアドレスを渡すことができます。
public class Client {
public static void main(String[] args)
throws NamingException, PrivilegedActionException, InterruptedException {
InitialContext ctx = new InitialContext(getCtxProperties());
String lookupName = "ejb:/server/HelloBean!ejb.HelloBeanRemote";
HelloBeanRemote bean = (HelloBeanRemote)ctx.lookup(lookupName);
System.out.println(bean.hello());
ctx.close();
}
public static Properties getCtxProperties() {
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, WildFlyInitialContextFactory.class.getName());
props.put(Context.PROVIDER_URL, "remote+http://127.0.0.1:8080");
props.put(Context.SECURITY_PRINCIPAL, "joe");
props.put(Context.SECURITY_CREDENTIALS, "joeIsAwesome2013!");
return props;
}
}
ルックアップに使用される初期コンテキストファクトリーは org.wildfly.naming.client.WildFlyInitialContextFactory です。
5.2.2. リモート Jakarta Enterprise Beans 設定ファイル
JBoss EAP は Elytron セキュリティーフレームワークを特長としています。クライアントアプリケーションのクラスパスの META-INF/ ディレクトリーにある wildfly-config.xml ファイルは、Elytron セキュリティーフレームワークおよび Jakarta Enterprise Beans クライアント設定に幅広い認証および承認オプションを許可します。
<configuration>
<authentication-client xmlns="urn:elytron:client:1.2">
<authentication-rules>
<rule use-configuration="default" />
</authentication-rules>
<authentication-configurations>
<configuration name="default">
<sasl-mechanism-selector selector="DIGEST-MD5" />
<set-user-name name="admin" />
<credentials>
<clear-password password="password123!" />
</credentials>
</configuration>
</authentication-configurations>
</authentication-client>
<jboss-ejb-client xmlns="urn:jboss:wildfly-client-ejb:3.0">
<connections>
<connection uri="remote+http://127.0.0.1:8080" />
</connections>
</jboss-ejb-client>
</configuration>
初期コンテキストに PROVIDER_URL、SECURITY_PRINCIPAL、および SECURITY_CREDENTIALS パラメーターを組み込む代わりに、wildfly-config.xml ファイルの <connection-uri> と <authentication-client> 要素を使用して接続 URI とセキュリティーを設定できます。
5.2.3. ClientTransaction アノテーション
@org.jboss.ejb.client.annotation.ClientTransaction アノテーションは、Jakarta Enterprise Beans クライアントからのトランザクション伝搬を処理します。クライアントにトランザクションがない場合には伝搬に失敗し、クライアントがアクティブであってもトランザクションの伝搬を防ぐことができます。org.jboss.ejb.client.annotation.ClientTransactionPolicy インターフェイスの定数を使用して、ClientTransaction アノテーションのポリシーを制御できます。以下は、org.jboss.ejb.client.annotation.ClientTransactionPolicy インターフェイスの定数です。
- MANDATORY: クライアント側のトランザクションコンテキストがない場合は例外で失敗します。クライアント側のトランザクションコンテキストが存在する場合は伝播します。
- NEVER: トランザクションコンテキストを伝搬することなく起動。クライアント側のトランザクションコンテキストが存在する場合は、例外が発生します。
- NOT_SUPPORTED: クライアント側のトランザクションコンテキストが存在するかどうかにかかわらず、トランザクションコンテキストを伝搬せずに起動します。
- SUPPORTS: クライアント側のトランザクションコンテキストがない場合にトランザクションなしで起動します。クライアント側のトランザクションコンテキストが存在する場合は伝播します。
アノテーションがない場合、デフォルトのポリシーは org.jboss.ejb.client.annotation.ClientTransactionPolicy#SUPPORTS になります。存在する場合は、トランザクションのありなしに関係なく、伝搬は失敗しません。
@ClientTransaction(ClientTransactionPolicy.MANDATORY)
@Remote
public interface RemoteCalculator {
public void callRemoteEjb() { }
}
@Stateless
@Remote(RemoteCalculator.class)
public class CalculatorBean implements RemoteCalculator {
@Override
public void callRemoteEjb() { }
}アノテーションでは、リモートインターフェイスプロバイダーがメソッドにトランザクションが必要であるかどうかをリモートインターフェイスのコンシューマーに指示できるようにします。