スケジュール間隔を構成するにはどうすればよいですか:
@Schedule(persistent=true, minute="*", second="*/5", hour="*")
アプリケーションコードの外?
- ejb-jar.xml でどのように構成できますか?
- アプリケーションの外部 (プロパティ ファイルのようなもの) で構成できますか?
デプロイメント記述子でのスケジューリングの例を次に示します。
<session>
<ejb-name>MessageService</ejb-name>
<local-bean/>
<ejb-class>ejb.MessageService</ejb-class>
<session-type>Stateless</session-type>
<timer>
<schedule>
<second>0/18</second>
<minute>*</minute>
<hour>*</hour>
</schedule>
<timeout-method>
<method-name>showMessage</method-name>
</timeout-method>
</timer>
</session>
タイマーを構成する別の方法は、プログラムによるスケジューリングです。
@Singleton
@Startup
public class TimedBean{
@Resource
private TimerService service;
@PostConstruct
public void init(){
ScheduleExpression exp=new ScheduleExpression();
exp.hour("*")
.minute("*")
.second("*/10");
service.createCalendarTimer(exp);
}
@Timeout
public void timeOut(){
System.out.println(new Date());
System.out.println("time out");
}
}
ejb-jar.xml
EJB 3.1 仕様に従って、アノテーションまたはデプロイメント記述子を介して自動タイマーを構成できます。
18.2.2 自動タイマー作成
タイマー サービスは、Bean クラスまたはデプロイメント記述子のメタデータに基づくタイマーの自動作成をサポートします 。これにより、Bean 開発者は、タイマー サービス タイマー作成メソッドの 1 つをプログラムで呼び出す Bean 呼び出しに依存することなく、タイマーをスケジュールできます。自動的に作成されるタイマーは、アプリケーションのデプロイの結果としてコンテナーによって作成されます。
そして、展開記述子 XLM スキーマについての私の理解は、<timer>
要素内の<session>
要素を使用してそれを定義するということです。
<xsd:element name="timer"
type="javaee:timerType"
minOccurs="0"
maxOccurs="unbounded"/>
詳細については、複合型の定義を参照timerType
してください (特にschedule
andtimeout-method
要素)。
私にとっては、ejb-jar.xml バリアントが TomEE でのみ動作し始めました。タイムアウト メソッドで javax.ejb.Timer パラメータを渡します。
<session>
<ejb-name>AppTimerService</ejb-name>
<ejb-class>my.app.AppTimerService</ejb-class>
<session-type>Singleton</session-type>
<timer>
<schedule>
<second>*/10</second>
<minute>*</minute>
<hour>*</hour>
</schedule>
<timeout-method>
<method-name>timeout</method-name>
<method-params>
<method-param>javax.ejb.Timer</method-param>
</method-params>
</timeout-method>
</timer>
public class AppTimerService {
public void timeout(Timer timer) {
System.out.println("[in timeout method]");
}
}
https://blogs.oracle.com/arungupta/entry/totd_146_understanding_the_ejb投稿に感謝します。
.properties ファイルを読み込んで、プログラムで Timer を作成できます
ScheduleExpression schedule = new ScheduleExpression();
schedule.hour(hourProperty);//previously read property from .properties file
schedule.minute(minuteProperty);//previously read property from .properties file
Timer timer = timerService.createCalendarTimer(schedule);
しかし、EJB で cron 式を使用できるかどうかわかりません。