i'm facing a problem when i run my apps in localhost. my application is tomcat based application which build with spring web MVC and use @Autowired DataSource as connection path to Database. besides that i have a apps feature to work with Java Quartz.
DataJdbc1.class :
@Repository
public class DataJdbc1 implements Data {
@Autowired
public DataSource dataSource1;
public JdbcTemplate jdbc1;
@Autowired
public DataSource dataSource2;
public JdbcTemplate jdbc2;
public void setDataSource1(DataSource dataSource1) {
this.dataSource1 = dataSource1;
}
public void setDataSource2(DataSource dataSource2) {
this.dataSource2 = dataSource2;
}
}
application-context.xml :
<bean id="dataSource1" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/xxx" />
</bean>
<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource1" />
</bean>
<bean id="dataSource2" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/xxx2" />
</bean>
<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource2" />
</bean>
CopyTableCustomerHandleJob.class
public class CopyTableCustomerHandleJob extends QuartzJobBean{
@Autowired
QuartzServicesFactoryDao quartzServicesFactoryDao;
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
try {
quartzServicesFactoryDao = new QuartzServicesFactoryDaoImpl();
quartzServicesFactoryDao.getCopyTableCustomerHandle();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
quartz-context.xml :
<!-- job -->
<bean name="copyTableCustomerHandleJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="crm.spring.main.quartz.CopyTableCustomerHandleJob"/>
<property name="jobDataMap">
<map>
<entry key="quartzServicesFactoryDao" value-ref="quartzServicesFactoryDao"/>
</map>
</property>
<property name="durability" value="true" />
<!-- trigger -->
<bean id="copyTableCustomerHandleJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="copyTableCustomerHandleJob"/>
<property name="cronExpression" value="0 0/1 * * * ?"/>
</bean>
<!-- SchedulerFactoryBean -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="copyTableCustomerHandleJob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="copyTableCustomerHandleJobTrigger" />
</list>
</property>
</bean>
afterwards, I deciding to build that quartz Application inside Spring WEB MVC with Job class, Trigger Class, schedulefactory and creating quartz-context.xml unfortunately, i have problem when run this quartz application, i can't initialize DataSource property every time i running the quartz application. but strangely when I'm trying to call the quartz application method with spring webMVC RequestMapping DataSource Initialized Succesfully.
did anyone of stackoverflow member had ever faced similar problem with mine?. what kind of solution that i must to do to fix my problem. thank you very much.
Quartz job is created by Quartz, not Spring. So you could use SpringBeanJobFactory to automatically autowire quartz jobs using spring.
The following URL is helpful for you. Please check it out.
https://stackoverflow.com/a/15211030/7119031