S2サポーター (2006-04-29 (土) 19:04:37)
Question
S2RMIのドキュメント通りにサーバプログラムを起動しますが、
終了はCtrl+Cです。この終了ができるように(つまりサービスとして)
実装したいと思っています。
これがサーブレットならtomcatなどを使えばよいのでしょうが、
S2RMIではどのように実装すればいいかよくわかりません。
下記ページのようにThreadを使用する方法が一つ分かっていますが、
他に適切な方法はありますか?
http://seasarproject.g.hatena.ne.jp/ueyama/comment?date=20050528
Answer
1.stopメソッドをもつIStoppableインターフェースを作成します。
package examples.service.main; public interface IStoppable { void stop(); }
2.ServerMainクラスはIStoppableを実装します。
public class ServerMain implements IStoppable
3.ServerMainクラスにstopメソッドを実装します。
新しいスレッドを作成してstaticなexitメソッドを呼びます。
public void stop() { Thread destroyer = new Thread() { public void run() { ServerMain.exit(); } }; destroyer.start(); }
4.ServerMainクラスにexitメソッドを実装します。
コンテナをdestroyしてexitします。
public static void exit() { if (container != null) { System.out.println("デストロイヤー参上!"); container.destroy(); System.exit(0); } }
5.stopを呼び出すクライアントプログラムとdiconファイルを書きます。
public class RemotingStop { private static final String PATH="dicon/RemotingStop.dicon"; public static void main(String[] args) { S2Container container = S2ContainerFactory.create(PATH); container.init(); try { IStoppable service = (IStoppable)container.getComponent("remotingStopper"); service.stop(); } finally { container.destroy(); } } }
(dicon/RemotingStop.diconファイル抜粋) <component name="remotingStopper" class="examples.service.main. IStoppable"> <aspect>remoting</aspect> </component>
ServerMainを実行し、その後でRemotingStopを実行すれば終了します。
参考投稿
http://ml.seasar.org/archives/seasar-user/2006-February/005074.html
http://ml.seasar.org/archives/seasar-user/2006-February/005088.html