2011-10-19 18 views

Trả lời

9

Nếu bạn sử dụng Spring Framework bạn có thể làm cho lớp con từ org.springframework.scheduling.quartz.SchedulerFactoryBean và ghi đè afterPropertiesSet() phương pháp.

public class MySchedulerFactoryBean extends org.springframework.scheduling.quartz.SchedulerFactoryBean { 

    @Autowired 
    private @Value("${enable.quartz.tasks}") boolean enableQuartzTasks; 

    @Override 
    public void afterPropertiesSet() throws Exception { 
     if (enableQuartzTasks) { 
      super.afterPropertiesSet(); 
     } 
    } 
} 

Sau đó thay đổi khai báo nhà máy thành tệp xml và đặt thuộc tính "enable.quartz.tasks" trong tệp thuộc tính. Đó là tất cả.

Tất nhiên, thay vì sử dụng @Autowired bạn có thể viết và sử dụng phương pháp setter và thêm

<property name="enableQuartzTasks" value="${enable.quartz.tasks}"/> 

để khai MySchedulerFactoryBean trong xml.

2

No. Nhưng tệp thuộc tính không khởi động trình lập lịch biểu.

Trình lên lịch không bắt đầu cho đến khi/trừ khi một số mã gọi đến scheduler.start().

4

Bạn có thể tắt Quartz Scheduler nếu bạn sử dụng Spring Framework 3.1 để tạo và khởi động nó. Mở tập tin cấu hình Spring của tôi, tôi sử dụng tính năng cấu hình mới của mùa xuân 3.1 theo cách này:

<beans profile="production,test"> 
    <bean name="bookingIndexerJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> 
     <property name="jobClass" value="com.xxx.indexer.scheduler.job.BookingIndexerJob" /> 
     <property name="jobDataAsMap"> 
      <map> 
       <entry key="timeout" value="10" /> 
      </map> 
     </property> 
    </bean> 

    <bean id="indexerSchedulerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> 
     <property name="jobDetail" ref="bookingIndexerJob" /> 
     <property name="startDelay" value="1000" /> 
     <property name="repeatInterval" value="5000" /> 
    </bean> 

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
     <property name="triggers"> 
      <list> 
       <ref bean="indexerSchedulerTrigger" /> 
      </list> 
     </property> 
     <property name="dataSource" ref="ds_quartz-scheduler"></property> 
     <property name="configLocation" value="classpath:quartz.properties" /> 
     <property name="applicationContextSchedulerContextKey" value="applicationContext" /> 
    </bean> 
</beans> 

Chỉ khi nào tôi muốn bắt đầu Scheduler (ví dụ đối với môi trường sản xuất), tôi đặt 'mùa xuân .profiles.active' sở hữu hệ thống, với danh sách các hồ sơ hoạt động:

-Dspring.profiles.active = "sản xuất"

Thông tin thêm ở đây:

http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/

http://java.dzone.com/articles/spring-profiles-or-not

1

Tôi gặp sự cố tương tự: tắt lịch trình trong phạm vi kiểm tra. Đây là một phần của ứng dụng của tôiContext.xml

<task:annotation-driven scheduler="myScheduler" /> 
<task:scheduler id="myScheduler" pool-size="10" /> 

Và tôi đã tắt lịch biểu bằng thuộc tính 'chính' và Mockito. Đây là ứng dụng của tôiContext-test.xml

<bean id="myScheduler" class="org.mockito.Mockito" factory-method="mock" primary="true"> 
    <constructor-arg value="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"/> 
</bean> 

Hy vọng trợ giúp này!

1

vẻ có tài sản autoStartup trong org.springframework.scheduling.quartz.SchedulerFactoryBean vì vậy bạn có thể cấu hình nó trong xml cấu hình như thế này:

<bean id="quartzFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="autoStartup" value="${cron.enabled}"/> <property name="triggers"> <list> <ref bean="someTriggerName"/> </list> </property> </bean>

Nhờ https://chrisrng.svbtle.com/configure-spring-to-turn-quartz-scheduler-onoff

2

Cá nhân tôi giống như câu trả lời từ Demis Gallisto. Nếu bạn có thể làm việc với hồ sơ, đây sẽ là đề xuất của tôi.

Ngày nay, mọi người có nhiều khả năng thích làm việc với Chú thích nhất, do đó là phần bổ sung cho câu trả lời của anh ấy.

@Configuration 
@Profile({ "test", "prod" }) 
public class SchedulerConfig { 
    @Bean 
    // ... some beans to setup your scheduler 
} 

này sẽ kích hoạt chức năng lịch chỉ khi cấu hình test HOẶC prod đang hoạt động. Vì vậy, nếu bạn đặt một cấu hình khác, ví dụ: -Dspring.profiles.active=dev không có gì sẽ xảy ra.

Nếu vì một số lý do bạn không thể sử dụng phương pháp tiếp cận hồ sơ, ví dụ: chồng lên các cấu hình ...

Giải pháp từ miso.belica dường như cũng hoạt động.

Xác định thuộc tính. ví dụ. trong application.properties: dailyRecalculationJob.cron.enabled=false và sử dụng nó trong SchedulerConfig của bạn.

@Configuration 
public class SchedulerConfig { 
    @Value("${dailyRecalculationJob.cron.enabled}") 
    private boolean dailyRecalculationJobCronEnabled; 

    @Bean 
    public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory, Trigger trigger) throws 
    SchedulerFactoryBean factory = new SchedulerFactoryBean(); 
    factory.setAutoStartup(dailyRecalculationJobCronEnabled); 
    // ... 
    return factory; 
    } 
    // ... the rest of your beans to setup your scheduler 
} 
Các vấn đề liên quan