2017-04-30 3 views
0

cron式をパラメータ化し、 "名前が 'beanScheduleCronExample'のBeanを作成中にエラーが発生しました:Beanの初期化に失敗しました。そして

クーロン発現「(「$ {cron.expression}」1見出さ)6つのフィールドで構成しなければならない私はpost

次実測値:遭遇無効@Scheduled方法「cronジョブ」:ネストされた例外は、java.lang.IllegalStateExceptionであります

私は表現が、私は

を持っている場合にのみ、読まれているAnnotationConfigApplicationContextコンテキストは=新しいAnnotationConfigApplicationContext( SpringScheduleCronExample.class)のcronことを利用し、

は私の主な方法で定義する私が午前問題があり、。したい メインメソッドなしでサーバー上でこれを実行すると、誰もこれで私を助けてください。ここで

はあり cron.expression = */5 * * * *

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:task="http://www.springframework.org/schema/task" 
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 

    <task:annotation-driven /> 
    <util:properties id="applicationProps" location="application.properties" /> 
    <context:property-placeholder properties-ref="applicationProps" /> 
    <bean class="com.hemal.spring.SpringScheduleCronExample" /> 
</beans> 

マイSpringScheduleCronExample.javaこの

package com.hemal.spring; 

import java.util.Date; 
import java.util.concurrent.atomic.AtomicInteger; 

import org.springframework.context.annotation.AnnotationConfigApplicationContext; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 
import org.springframework.scheduling.annotation.EnableScheduling; 
import org.springframework.scheduling.annotation.Scheduled; 

@Configuration 
@EnableScheduling 
@PropertySource("classpath:application.properties") 
public class SpringScheduleCronExample { 
    private AtomicInteger counter = new AtomicInteger(0); 


    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 

     return new PropertySourcesPlaceholderConfigurer(); 
    } 

    @Scheduled(cron = "${cron.expression}") 
    public void cronJob() { 
     int jobId = counter.incrementAndGet(); 
     System.out.println("Job @ cron " + new Date() + ", jobId: " + jobId); 
    } 

    public static void main(String[] args) { 

      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
        SpringScheduleCronExample.class); 
     try { 
      Thread.sleep(24000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } finally { 
      context.close(); 
     } 
    } 
} 

私のアプリケーションのプロパティのように見える私のapplicationContext.xmlをですか?ここで

+0

あなたはウェブアプリケーションを作成する方法を知っていますか?そうでない場合は、[このチュートリアル](https://www.mkyong.com/maven/how-to-create-a-web-application-project-with-maven/)の最初の2つのステップを簡単な例で確認できます。 Webアプリケーションを使用している場合は、pom.xmlにspring依存関係を追加し、web.xmlにspringコンテキストを指定する必要があります。後者のチェックは[この質問](http://stackoverflow.com/questions/6451377/loading-context- in-spring-using-web-xml)を使用します。 –

+0

Czerwinski、私はWebアプリケーションの作成方法を知っています。私のアプリケーションはGUIを持たず、MDB経由でメッセージを処理し、Webサービスコールを行います。 Webサービスコールに失敗した場合、これらのメッセージを再度処理するエラーキューを持たずに、Webサービスコールを行う4時間ごとに実行するSpringスケジューラが必要です。私はこのプロセスを働かせています。私はcron式をプロパティファイルに移動し、このプロセスで課題に直面したいと思います。 – Hemal

+0

私はまだあなたが直面している問題を正確に理解していません。サーバー上でアプリケーションを実行すると、classpathに 'applicationContext.xml'と' application.properties'ファイルがありますか?コンテキストが正しくインスタンス化されている場合(最初のコメントの最後のリンクを見てください)、スケジューラーは正常に動作するはずです。 –

答えて

0

は、私はそれが

アプリケーションのcontext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:task="http://www.springframework.org/schema/task" 
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 


    <context:property-placeholder properties-ref="applicationProps" /> 
    <context:annotation-config/> 
    <context:component-scan base-package="com.hemal.spring" /> 
    <task:annotation-driven /> 
</beans> 

MyApplicationConfig.java

package com.hemal.spring; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.scheduling.annotation.EnableScheduling; 

@Configuration 
@EnableScheduling 
@ComponentScan(basePackages = {"com.hemal.spring"}) 
@PropertySource("classpath:application.properties") 
public class MyApplicationConfig { 

    @Bean 
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
     PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer(); 

     properties.setLocation(new ClassPathResource("application.properties")); 
     properties.setIgnoreResourceNotFound(false); 

     return properties; 
    } 
} 

MyApplicationContext.java

パッケージcom.hemalを動作するようになった方法です。春;

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 

import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.ContextLoaderListener; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 

public class MyApplicationContext implements WebApplicationInitializer{ 

    public void onStartup(ServletContext servletContext) throws ServletException { 
     AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 
     rootContext.register(MyApplicationConfig.class); 


     servletContext.addListener(new ContextLoaderListener(rootContext)); 
    } 
} 

マイスケジューラクラス

package com.hemal.spring; 

import java.util.Date; 
import java.util.concurrent.atomic.AtomicInteger; 

import org.springframework.context.annotation.AnnotationConfigApplicationContext; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 
import org.springframework.scheduling.annotation.EnableScheduling; 
import org.springframework.scheduling.annotation.Scheduled; 


public class SpringScheduleCronExample { 
    private AtomicInteger counter = new AtomicInteger(0); 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 

     return new PropertySourcesPlaceholderConfigurer(); 
    } 

    @Scheduled(cron = "${cron.expression}") 
    public void cronJob() { 
     int jobId = counter.incrementAndGet(); 
     System.out.println("Job @ cron " + new Date() + ", jobId: " + jobId); 
    } 

    public static void main(String[] args) { 
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
        SpringScheduleCronExample.class); 
     try { 
      Thread.sleep(24000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } finally { 
      context.close(); 
     } 
    } 
} 

application.properties cron.expression = 0/5 * * * *?

関連する問題