BlazeDSでHello World

FlexJavaを連携させるために、BlazeDSを使ってみます。
BlazeDSservletです。どんな実装をしているのか気になるところですが、とりあえず動かしてみます。

準備

出来上がりイメージ

こんな感じで、テキストインプットに入力した文字の先頭に、"Hello, "を付け加えて返却するサーバサービスとFlex画面を連携させます。

サーバ側プロジェクトを作成

eclipse上で、Dynamic Web Projectを作成します。
プロジェクト名:hello_blazeds_server
ターゲットランタイム:blazeds-turnkey付属のtomcat 6
サーブレットバージョン:2.5

lib
プロジェクトを作成したら、WEB-INF/libの下に、blazedsで必要なライブラリを格納します。
blazeds-turnkey-3.2.0.3978/tomcat/webapps/samples/WEB-INF/libの下に入っているので、それを持ってきます。ここでは、以下の4つをコピーしました。

  • backport-util-concurrent.jar
  • flex-messaging-common.jar
  • flex-messaging-core.jar
  • flex-messaging-remoting.jar

web.xml
まず、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>hello_blazeds_server</display-name>
	<welcome-file-list>
		<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>

	<!-- ▼▼▼ Listener ▼▼▼ -->
	<listener>
		<listener-class>flex.messaging.HttpFlexSession</listener-class>
	</listener>
	<!-- ▲▲▲ Listener ▲▲▲ -->

	<!-- MessageBroker Servlet -->
	<!-- ▼▼▼ Servlet ▼▼▼ -->
	<servlet>
		<servlet-name>MessageBrokerServlet</servlet-name>
		<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
		<init-param>
			<param-name>services.configuration.file</param-name>
			<param-value>/WEB-INF/flex/services-config.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>MessageBrokerServlet</servlet-name>
		<url-pattern>/messagebroker/*</url-pattern>
	</servlet-mapping>
	<!-- ▲▲▲ Servlet ▲▲▲ -->
</web-app>

services-config.xml
次にFlex用の設定ファイルを作成していきます。
WebContent/WEB-INFの下に「flex」フォルダを作成します。そのフォルダの中にservices-config.xmlを作成します。
services-config.xmlではサービスの設定をするのですが、service-includeタグを使うことで、部分的に定義を外部化できるようです。ここでは、Javaサービスの設定をremoting-config.xmlに外部化しました。

(WebContent/WEB-INF/flex/services-config.xml)

<?xml version="1.0" encoding="UTF-8"?>
<services-config>
	<services>
		<service-include file-path="remoting-config.xml" />
	</services>
	
	<channels>
		<channel-definition id="my-amf"
			class="mx.messaging.channels.AMFChannel">
			<endpoint
				url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf"
				class="flex.messaging.endpoints.AMFEndpoint" />
			<properties>
				<polling-enabled>false</polling-enabled>
			</properties>
		</channel-definition>
	</channels>
</services-config>

remoting-config.xml
services-config.xmlから外部化したリモートサービスの設定を行います。デフォルトのチャネルをservices-config.xmlで定義した"my-amf"にしておきます。タグでは呼出し先のJavaサービスを記述します。

(WebContent/WEB-INF/flex/remoting-config.xml)

<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service" class="flex.messaging.services.RemotingService">
	<adapters>
		<adapter-definition id="java-object"
			class="flex.messaging.services.remoting.adapters.JavaAdapter"
			default="true" />
	</adapters>

	<default-channels>
		<channel ref="my-amf" />
	</default-channels>

	<destination id="<span style="color:#FF0000;font-weight:bold;">hello</span>">
		<properties>
			<source>sample.hello.HelloService</source>
		</properties>
	</destination>
</service>

Javaサービス
Flex画面側から呼出すメソッドを定義します。

(src/sample/hello/HelloService.java)

package sample.hello;

public class HelloService {
	public String sayHello(String name) {
		return "hello, " + name;
	}
}

flex側プロジェクト作成

RemoteObjectを定義しておき、destination属性にremoting-config.xmlで定義したhelloを指定します。endpoint属性には、services-config.xmlで定義したチャネルのendpointを指定します。services-config.xmlと違って、変数によるバインドが効かないので、ここでは設定値をベタ書きにしています。
blazeds-turnkeyに付属するtomcatのポート番号は8400で設定されているので注意してください。
ボタンクリック時のイベントとしてsayHelloメソッドの呼出しを指定します。

(src/HelloBlazeDS.mxml)

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:RemoteObject
		id="service"
		destination="hello"
		endpoint="http://localhost:8400/hello_blazeds_server/messagebroker/amf"/>
	<mx:TextInput x="10" y="20" id="helloText"/>
	<mx:Button x="178" y="20" label="Send" click="service.sayHello(helloText.text)"/>
	<mx:Label x="10" y="50" text="{service.sayHello.lastResult}"/>
</mx:Application>

完成

以上でhelloサンプルの完成。
blazedsのサンプルをみると、services-config.xmlのほかに、messaging-config.xmlやproxy-config.xml、remoting-config.xmlが入っているけど、実際に必要なのはservices-config.xmlだけでした。他の定義ファイルは、services-config.xmlの定義を外部化しただけなんですね。

参考

http://blog.isocchi.com/2008/04/javaflexblazedsflexjava.html
http://www.stbbs.net/blog/2008/01/blazedsweb.html