LoginSignup
3
5

More than 5 years have passed since last update.

JBossAS7でのリモートEJB呼び出し

Posted at

備忘録です。需要はなさそうですが。
使用したのはjboss-as-7.1.1.FinalでWindows環境下。
JBoss側はデフォルトのStandaloneで起動。設定は特になし。

サーバ

まずリモートEJB。インタフェースは省略。

HelloRemoteBean.java
package sample.service;

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

/**
 * Session Bean implementation class HelloRemote
 */
@Remote
@Stateless
public class HelloRemoteBean implements HelloRemote {

    public void sayHello(){
        // リモートの標準出力なので注意
        System.out.println("Hello Remote!");
    }

}

EAR作るので、application.xmlも。

application.xml
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  version="6" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd">

  <application-name>SampleEAR</application-name>
  <initialize-in-order>true</initialize-in-order>
  <module>
    <ejb>SampleEJB.jar</ejb>
  </module>
</application>

クライアント

POJOで作ります。完全に確認用。

SampleClient.java
package sample.client;

import java.util.Properties;

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

import sample.service.HelloRemote;

public class SampleClient {
    public static void main(String args[]) throws Exception
    {
        new SampleClient().callRemote();
    }

    public void callRemote() throws NamingException{
        Hashtable<String,String> envs = new Hashtable<String,String>();
        envs.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

        InitialContext context = new InitialContext(envs);

        try{
            // jboss-ejb-client.propertiesの読込確認
            System.out.println("Properties is " + this.getClass().getClassLoader().getResourceAsStream("jboss-ejb-client.properties"));
            HelloRemote remote=(HelloRemote)context.lookup("ejb:SampleEAR/SampleEJB//HelloRemoteBean!sample.service.HelloRemote");
            remote.sayHello();
        }finally{
            context.close();
        }

    }
}

SSL通信、URL等の設定をjboss-ejb-client.propertiesに記述します。
ファイル自体はクライアントJARの直下に配置。

jboss-ejb-client.properties
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

実行

コマンドも一応。
java -cp SampleClient.jar\;SampleEJB.jar;<JBOSS_HOME>\bin\client\jboss-client.jar jp.co.sample.client.SampleClient
#うっかり-jarを指定して半日つぶした

以下がJBossのログに出力されていればOK。

23:51:14,171 INFO [stdout] (EJB default - 1) Hello Remote!


参考

*Minimum Jars Required for Client app to remotely Invoke EJBs in JBoss AS7
*EJB invocations from a remote client using JNDI

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5