menu
書いてる野郎
orebike@gmail.com
Webアプリでもバッチ処理とかcronでServiceを動かしたいとかいろいろあると思うので、コマンドラインで実行できるようにしたい
tomcat を内部から curl で叩くという方法もあるだろうが、筋としては違うと思う。
以下メモ。後でまとめるよ。
eclipseのDoltengプロジェクトでスタンドアロンアプリを作成。
convention.diconの書き換え
<components> <component class="org.seasar.framework.convention.impl.NamingConventionImpl"> <!-- SAStruts側のプロジェクトのDI管理系の設定を追記 --> <initMethod name="addRootPackageName"> <arg>"hoge.piyo.fugasastruts"</arg> </initMethod> <initMethod name="addRootPackageName"> <arg>"hoge.piiyo.fugacommand"</arg> </initMethod> </component> <!-- S2JDBCを使う場合に必要な記述 --> <component class="org.seasar.framework.convention.impl.PersistenceConventionImpl"/> </components>
app.diconに必要な記述の追加と、実行のエントリーポイントになるクラス記述を追加 SAStruts側プロジェクトからのdiconファイルの移植
<include path="j2ee.dicon"/> <include path="s2jdbc.dicon"/> <component name="helloWorld" class="hoge.piiyo.fugacommand.command.HelloWorld" instance="singleton" />
実行クラスの作成
public class HelloWorld{ // SAStruts側プロジェクトにあるServiceだけど、この記述でちゃんと入る @Resource public HogeService hogeService; // エントリーポイント public static void main(String[] args){ SingletonS2ContainerFactory.init(); HelloWorld helloWorld = SingletonS2Container.getComponent(HelloWorld.class); helloWorld.execute(); } // 実行本体 public void execute(){ System.out.println(hogeService.getName()); } }
スタンドアロンアプリを作った場合は↑でよし
もうすでにSAStrutsの何かのプロジェクトがあってそのなかにコマンド用の何かを入れるならばコンテナの取得方法が若干違う。 このようになる。
public static void main(String[] args){ S2ContainerFactory.configure("app.dicon"); S2Container s2container = S2ContainerFactory.getConfigurationContainer(); HelloWorldCommand helloWorldCommand = (HelloWorldCommand) s2container.getComponent(HelloWorldCommand.class); helloWorldCommand.execute(); }
キャストがウザイが汚れるのがここだけなので別にいいや。