2016-02-17 46 views
0

這裏是我的 'GenericQuartzJob' 類春天Quartz調度不訪問數據庫

public class GenericQuartzJob extends QuartzJobBean 
{ 

    private String batchProcessorName; 

    public String getBatchProcessorName() { 
     return batchProcessorName; 
    } 

    public void setBatchProcessorName(String name) { 
//  System.out.println("jo"+name); 
     this.batchProcessorName = name; 
    } 

    protected void executeInternal(JobExecutionContext jobCtx) throws JobExecutionException 
    { 
     try { 
//   System.out.println("jo"); 
      SchedulerContext schedCtx = jobCtx.getScheduler().getContext(); 

      ApplicationContext appCtx = 
       (ApplicationContext) schedCtx.get("applicationContext"); 
      java.lang.Runnable proc = (java.lang.Runnable) appCtx.getBean(batchProcessorName); 
      proc.run(); 
     } 
     catch (Exception ex) { 
//   ex.printStackTrace(); 
      throw new JobExecutionException("Unable to execute batch job: " + batchProcessorName, ex); 
     } 
    } 
} 

這裏是我的類

@Configuration 
@ComponentScan("com.ehydromet.service") 
public class MyRunnableJob implements Runnable { 

    //@Resource private SpringService myService; 
@Autowired 
public UserService service; 
    @Override 
    public void run() { 
//  System.out.println("hu"); 
     try{ 
     service.getAllAdminUser(); 
     }catch(Exception ex){ 
      System.out.println(ex.getMessage()+" MyRunnableJob"); 
     }MqttImplement mqtt=new MqttImplement(); 
     mqtt.publishMessage(); 
     //Run the Job. This code will run in the Spring context so you can use injected dependencies here. 
    } 
} 

下面是我MqttImplement

public class MqttImplement implements MqttCallback { 
@Autowired 
private static UserService service; 
@RequestMapping("/publish") 
    public void publishData(ModelMap map){ 
     MyTask.map=map; 
     pubTopic="tmsdata"; 
     susTopic="tmsack"; 
     if(initializeMqTTParameters() ){ 
      if(suscribeForAck()){ 
//    new ClassPathXmlApplicationContext("spring-quartz.xml");  
       AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); 
      } 
     } 


//  initializeMqTTParameters(); 
//  suscribeForAck(); 
//  publishMessage(); 

    } 
    public void publishMessage(){ 

     try{ 


//  System.out.print("sending "); 
      Received received=service.getReceivedDataToPublishTMS(); 
     if(received!=null){ 
      System.out.print("sending you not null"); 
      String publisgMSG=createMessageToPublish(received); 
      pubMessage="lava," ; 
      publishMessageTms(); 
     }else{ 
      System.out.println("null hora"); 
     } 
     }catch(Exception ex){ 
      System.out.println("hora"+ex.getLocalizedMessage()+" "+ex.toString()); 
     } 
    } 
} 

,這裏是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:context="http://www.springframework.org/schema/context" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 

<!--  <bean id="exampleBusinessObject" class="com.ehydromet"/> --> 

    <!-- Old JobDetail Definition 
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
     <property name="targetObject" ref="exampleBusinessObject"/> 
     <property name="targetMethod" value="doIt"/> 
    </bean> 
    --> 

    <!-- Runnable Job: this will be used to call my actual job. Must implement runnable --> 
    <bean name="myRunnableJob" class="com.ehydromet.controller.MyRunnableJob"></bean> 

    <!-- New Clusterable Definition --> 
    <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean"> 
     <property name="jobClass" value="com.ehydromet.schedule.GenericQuartzJob" /> 
     <property name="jobDataAsMap"> 
      <map> 
       <entry key="batchProcessorName" value="myRunnableJob" /> 
      </map> 
     </property> 
    </bean> 

    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> 
     <!-- see the example of method invoking job above --> 
     <property name="jobDetail" ref="jobDetail"/> 
     <!-- 10 seconds --> 
     <property name="startDelay" value="5000"/> 
     <!-- repeat every 50 seconds --> 
     <property name="repeatInterval" value="5000"/> 
    </bean> 
<context:component-scan base-package="com.ehydromet.service"/> 
     <context:component-scan base-package="com.ehydromet.dao"/> 
     <context:component-scan base-package="com.ehydromet.service.impl"/> 
     <context:component-scan base-package="com.ehydromet.dao.impl"/> 
     <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="annotatedClasses"> 
      <list> 
      <value>com.ehydromet.entity.Alarms</value> 
       <value>com.ehydromet.entity.UserData</value> 
       <value>com.ehydromet.entity.ModemInfo</value> 
       <value>com.ehydromet.entity.Received</value> 
       <value>com.ehydromet.entity.Modems</value> 
       <value>com.ehydromet.entity.AdminLogin</value> 
       <value>com.ehydromet.entity.UserPolicy</value> 

      </list> 
     </property> 

     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.hbm2ddl.auto">validate</prop> 
      </props> 
     </property> 
    </bean> 
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
     <property name="sessionFactory"> 
      <ref bean="sessionFactory"/> 
     </property> 
     <property name="triggers"> 
      <list> 
       <ref bean="simpleTrigger" /> 
      </list> 
     </property> 
     <!-- Add this to make the Spring Context Available --> 
     <property name="applicationContextSchedulerContextKey"><value>applicationContext</value></property> 

    </bean> 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/ejava" /> 
     <property name="username" value="root" /> 
     <property name="password" value="" /> 
    </bean> 



    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
    <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor"> 
     <property name="sessionFactory"> 
      <ref bean="sessionFactory"/> 
     </property> 
    </bean> 
</beans> 

Dao類別爲@Transactional@Repository

MqttImplement類服務對象爲空我認爲自動裝配不與Quartz調度程序一起工作。任何建議?
我認爲autowired是一種解決。但 錯誤是 - org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [org.springframework.scheduling.quartz.SchedulerFactoryBean]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? 我是新來的。所以可能會犯一些愚蠢的錯誤,請大家看看。

回答

0

你試過@Autowired沒有static?似乎不可思議,我...

從對方你這樣做:

MqttImplement mqttPublishTms=new MqttImplement(); 
................ 
mqttPublishTms.publishMessage(); 

因此,它不new依賴注入。毫不奇怪,service財產有null

+0

但我也嘗試在類MyTask中調用服務對象service.getAllAdminUser(); – Rawat

+0

我不知道爲什麼我的「靜態」消息不擔心你... –

+0

因爲我也試過它沒有靜態關鍵字,我用它因爲我想知道「新」關鍵字不會注入依賴關係,所以我使publishMessage類「靜態」,然後嘗試它,但沒有工作,我忘了刪除它,所以....... – Rawat