spring定時任務相關

qixiaobo發表於2018-01-05

title: spring定時任務相關 tags: categories: 工作日誌 date: 2016-08-25 18:18:55

目前環境中定時任務使用比較常見,對使用quartz的用法不做太多說明,簡述一下分散式環境下quartz的做法(基於沒有單獨的quartz server)

  1. maven環境下定義分散式環境下主從角色

    <profile>

        <id>product</id>

        <properties>

            <data.version>1.0.8</data.version>

            <erp.token.validate>true</erp.token.validate>

            <upyun.env>prod</upyun.env>

            <exception.mail.subject>f6-ERROR-prod</exception.mail.subject>

            <exception.mail.threshold>ERROR</exception.mail.threshold>

        </properties>

    </profile>

    <profile>

        <id>product-slave</id>

        <properties>

            <data.version>1.0.8</data.version>

            <erp.token.validate>true</erp.token.validate>

            <upyun.env>prod</upyun.env>

            <quartz.deleteXml>applicationContext-quarz</quartz.deleteXml>

            <exception.mail.subject>f6-ERROR-prod-slave</exception.mail.subject>

            <exception.mail.threshold>ERROR</exception.mail.threshold>

        </properties>

    </profile>

  2. 對於不同角色下面master可以呼叫定時任務,slave不呼叫定時任務
    因此在maven打包是從角色刪除定時任務配置檔案

    <build>

        <plugins>

            <plugin>

                <artifactId>maven-antrun-plugin</artifactId>

                <dependencies>

                    <dependency>

                        <groupId>ant</groupId>

                        <artifactId>ant-nodeps</artifactId>

                        <version>1.6.5</version>

                    </dependency>

                </dependencies>

                <executions>

                    <execution>

                        <id>delete</id>

                        <phase>compile</phase>

                        <goals>

                            <goal>run</goal>

                        </goals>

                        <configuration>

                            <tasks>

                                <echo>

                                    process delete ${quartz.deleteXml}.xml;

                                </echo>

                                <delete>

                                    <fileset dir="${basedir}/target/classes/"

                                             includes="${quartz.deleteXml}.xml"/>

                                </delete>

                            </tasks>

                        </configuration>

                    </execution>

                </executions>

            </plugin>

        </plugins>

    </build>

    由於不同profile下檔案quartz.deleteXml的值不同,因此可以按照環境刪除指定的檔案

  3. 使用jmx呼叫quartz任務

    QuartzMBean

    import org.springframework.stereotype.Component;

     

    import java.lang.reflect.InvocationTargetException;

    import java.lang.reflect.Method;

     

    /**

     * Created by qixiaobo on 16/8/2.

     */

    @Component

    public class QuartzMBean {

     

     

        public String invokeJob(String beanName, String methodName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

            Object bean = SpringHelper.getBean(beanName);

            if (bean == null) {

                return beanName + " not exist";

            }

            Method method = bean.getClass().getMethod(methodName);

            method.invoke(bean);

            return "success!";

        }

     

     

        public String invokeJob( String methodName) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {

            F6MonitorJob job = SpringHelper.getBean(F6MonitorJob.class);

            Method method = job.getClass().getMethod(methodName);

            method.invoke(job);

            return "success!";

        }

    }

    增加相關jmx配置

    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">

        <property name="locateExistingServerIfPossible" value="true"/>

    </bean>

     

    <!--spring jmx -->

    <!-- this bean must not be lazily initialized if the exporting is to happen -->

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"

          lazy-init="false">

        <property name="beans">

            <map>

                <entry key="erp:name=rmiRegisterBean" value-ref="rmiRegisterMBean" />

                <entry key="erp:name=cacheBean" value-ref="cacheMBean" />

                <entry key="erp:name=quartzBean" value-ref="quartzMBean" />

            </map>

        </property>

        <property name="registrationPolicy" value="IGNORE_EXISTING" />

    </bean>

相關文章