如何在JBoss ESB上创建一个非常简单的cron作业

时间:2011-02-25 03:21:04

标签: cron jboss-esb

我正在寻找最简单的工作来安排一个简单的工作 - 一个不与任何其他ESB组件交互的工作。 [这是我在ESB-land的第一个小时。“]

我对cron-scheduler制作人有点了解。如果您熟悉cron

,它看起来很简单
<providers>
  <schedule-provider name="schedule">
    <cron-schedule scheduleid="cron-trigger" cronExpression="0/1 * * * * ?" />
  </schedule-provider>
</providers>

接下来有一个监听器(引用生产者)来处理预定的事件

<services>
    <service category="ServiceCat" name="ServiceName" description="Test Service">
        <listeners>
            <scheduled-listener name="cron-schedule-listener" 
                                scheduleidref="cron-trigger"
                                event-processor="org.example.MyListener" />
        </listeners>
    </service>
</services> 

鉴于我的工作行动不会发送任何消息,或者需要通知任何有关成功或失败的信息。我可以简单地扩展ScheduleListener并覆盖onSchedule()方法并在那里实现我的作业执行而不创建动作吗?

即使这是可能的,对这个简单模式使用动作会有什么好处吗?

我正在使用JBoss ESB 4.9。

2 个答案:

答案 0 :(得分:2)

我受到了惩罚并添加了我自己几乎空的ScheduledEventMessageComposer并让我的工作延长AbstractActionLifecycle

jboss-esb.xml

<?xml version="1.0"?>
<jbossesb parameterReloadSecs="300"
 xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.0.1.xsd"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.0.1.xsd http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.0.1.xsd">
 <providers>
  <schedule-provider name="schedule">
  <!-- fire every hour -->
   <cron-schedule cronExpression="0 1 * * * ?" scheduleid="cron-trigger"/>
  </schedule-provider>
 </providers>
 <services>
  <service category="dummy" description="create dummy message" name="dummy-message">
   <listeners>
    <scheduled-listener event-processor="org.example.ActionMsgComposer"
     name="cron-schedule-listener" scheduleidref="cron-trigger"/>
   </listeners>
   <actions mep="OneWay">
    <action class="org.example.CronAction" name="action" process="execute"/>
   </actions>
  </service>
 </services>
</jbossesb> 

org.example.ActionMsgComposer

package org.example;

import org.jboss.soa.esb.ConfigurationException;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.listeners.ScheduledEventMessageComposer;
import org.jboss.soa.esb.message.Message;
import org.jboss.soa.esb.message.format.MessageFactory;
import org.jboss.soa.esb.message.format.MessageType;
import org.jboss.soa.esb.schedule.SchedulingException;

public class ActionMsgComposer implements ScheduledEventMessageComposer {

    public void initialize(ConfigTree config) throws ConfigurationException {
    }

    public void uninitialize() {
    }

    public Message composeMessage() throws SchedulingException {
        Message myMessage = MessageFactory.getInstance().getMessage(MessageType.JBOSS_XML);
        myMessage.getBody().add("dummy");
        return myMessage;
    }

    public Message onProcessingComplete(Message message)
            throws SchedulingException {
        return message;
    }

}

org.example.CronAction

package org.example;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;

import org.jboss.soa.esb.actions.AbstractActionLifecycle;
import org.jboss.soa.esb.actions.ActionLifecycleException;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;

public class CronAction extends AbstractActionLifecycle {

    private ConfigTree configTree;
    private boolean initialized = false;
    private final Lock initLock = new ReentrantLock(true);
    private final Condition initCondition = initLock.newCondition();

    public CronAction(ConfigTree config) {
        configTree = config;
    }

    public ConfigTree getConfigTree() {
        return configTree;
    }

    @Override
    public void initialise() throws ActionLifecycleException {
        try {
            initLock.lock();
            super.initialise();
            // ** do any one-time initialization for cron job here **
            initialized = true;
            initCondition.signalAll();
        } finally {
            initLock.unlock();
        }
    }

    public Message exec(Message message) {
        waitUntilInitialized();
        // ** do cron job here ** 
        return message;
    }

    @Override
    public void destroy() throws ActionLifecycleException {
        super.destroy();
        // ** do any clean-up for cron job here **
    }

    private void waitUntilInitialized() {
        if (!initialized) {
            try {
                initLock.lock();
                while (!initialized) {
                    initCondition.await();
                }
            } catch (InterruptedException e) {
                // ignore
            } finally {
                initLock.unlock();
            }
        }
    }

}

答案 1 :(得分:2)

另一种方法是实现org.jboss.soa.esb.schedule.ScheduledEventListener类。

因此你的jboss-esb.xml看起来像这样:

<?xml version="1.0"?>
<jbossesb parameterReloadSecs="5" xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.3.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.3.0.xsd http://anonsvn.jboss.org/repos/labs/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.3.0.xsd">
 <providers>
  <schedule-provider name="myDaemonProvider">
   <cron-schedule cronExpression="0 */1 * * * ?" scheduleid="my-daemon-trigger"/>
  </schedule-provider>
 </providers>
 <services>
  <service category="Cron" description="My Cron Daemon" name="MyDaemon">
   <listeners>
    <scheduled-listener event-processor="com.myCompany.esb.myDaemon.main.MyDaemonScheduledEventListener" name="my-daemon-listener"  scheduleidref="my-daemon-trigger"/>
   </listeners>
  </service>
 </services>
</jbossesb>

然后你的com.myCompany.esb.myDaemon.main.MyDaemonScheduledEventListener看起来像:

package com.myCompany.esb.myDaemon.main;

import org.jboss.soa.esb.ConfigurationException;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.schedule.ScheduledEventListener;
import org.jboss.soa.esb.schedule.SchedulingException;

public class MyDaemonScheduledEventListener implements ScheduledEventListener {

    public void initialize(ConfigTree arg0) throws ConfigurationException {
        System.out.println("Initializing the Cron Daemon...");
    }

    public void uninitialize() {
        System.out.println("Uninitializing the Cron Daemon...");
    }

    public void onSchedule() throws SchedulingException {
        System.out.println("Scheduled Execution!");
    }
}

因此,当您部署它时,它将导致onSchedule()每分钟运行一次。