Issue
I am exploring this project https://github.com/davidmarquis/redis-scheduler. I have setup on my local and am able to run the integration test cases. Now, I am trying to create an application end point to set some delayed task and start the polling thread. I have been able to get my task stored in a redis zset (via the RedisTaskScheduler->scheduleAt() method) but I am not able to get the polling process started which is supposed to remove the keys from the zset.
Following is what I did -
Following is an entry point I have created -
package com.github.davidmarquis.redisscheduler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.JedisPool;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;
public class Main {
private RedisTaskScheduler scheduler;
private JedisPool pool;
public static void main(String[] args) {
provideActors();
}
private static void provideActors() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");
RedisTaskScheduler scheduler = ctx.getBean(RedisTaskScheduler.class);
Long timestamp = 1593272685000l;
scheduler.scheduleAt("something_via_main", Instant.ofEpochSecond(timestamp));
Timestamp ts=new Timestamp(timestamp);
Date date=new Date(ts.getTime());
//ApplicationContext ctxAnno = new AnnotationConfigApplicationContext(AppConfig.class);
//pool = (JedisPool) ctxAnno.getBean("jedis");
//PollingThread thread = (PollingThread) ctxAnno.getBean(PollingThread.class);
System.out.println("Main method "+date);
}
public void stopScheduler() {
scheduler.stop();
}
public static void shutdown() {
//pool.close();
}
}
Following is the application-context.xml file I have put in resources -
<?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"
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-3.1.xsd">
<!-- <context:property-placeholder location="test-config.properties"/>-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"/>
<property name="port" value="6379"/>
<property name="database" value="0"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
</bean>
<bean id="springTemplateDriver" class="com.github.davidmarquis.redisscheduler.drivers.spring.RedisTemplateDriver">
<constructor-arg name="redisTemplate" ref="redisTemplate"/>
</bean>
<bean id="scheduler" class="com.github.davidmarquis.redisscheduler.RedisTaskScheduler" >
<constructor-arg name="driver" ref="springTemplateDriver"/>
<!-- <constructor-arg name="listener" ref="triggerListener"/>-->
<constructor-arg name="listener">
<bean class="com.github.davidmarquis.redisscheduler.Implementation"/>
</constructor-arg>
<property name="pollingDelayMillis" value="10"/> <!-- Speeds up polling for tests so that they run quicker. -->
</bean>
</beans>
21:46:04.215 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@359f7cdf: defining beans [jedisConnectionFactory,redisTemplate,springTemplateDriver,scheduler]; root of factory hierarchy
21:46:04.216 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'jedisConnectionFactory'
21:46:04.216 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'jedisConnectionFactory'
21:46:04.334 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'jedisConnectionFactory' to allow for resolving potential circular references
21:46:04.427 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'jedisConnectionFactory'
21:46:04.588 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'jedisConnectionFactory'
21:46:04.588 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'redisTemplate'
21:46:04.588 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'redisTemplate'
21:46:04.597 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'redisTemplate' to allow for resolving potential circular references
21:46:04.598 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'jedisConnectionFactory'
21:46:04.630 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.data.redis.serializer.StringRedisSerializer#323b36e0'
21:46:04.630 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.data.redis.serializer.StringRedisSerializer#323b36e0'
21:46:04.631 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'redisTemplate'
21:46:04.636 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'redisTemplate'
21:46:04.637 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'springTemplateDriver'
21:46:04.637 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'springTemplateDriver'
21:46:04.640 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'redisTemplate'
21:46:04.665 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'springTemplateDriver' to allow for resolving potential circular references
21:46:04.665 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'springTemplateDriver'
21:46:04.665 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'scheduler'
21:46:04.665 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'scheduler'
21:46:04.666 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'springTemplateDriver'
21:46:04.666 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'com.github.davidmarquis.redisscheduler.Implementation#4f18837a'
21:46:04.675 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'com.github.davidmarquis.redisscheduler.Implementation#4f18837a'
21:46:09.144 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'scheduler' to allow for resolving potential circular references
21:46:09.150 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'scheduler'
21:46:09.152 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@2c78d320]
21:46:09.152 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
21:46:09.156 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
21:46:09.160 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'scheduler'
21:46:11.310 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Opening RedisConnection
21:46:14.382 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Closing bound connection.
Main method Sat Jun 27 21:14:45 IST 2020
Upon debugging I found that the start() method of the RedisTaskScheduler, which is annotated with PostConstruct, is not being called.
Solution
Please add <context:annotation-config/>
(or context:component-scan/) to enable @PostConstruct
handling.
Answered By - Amit kumar
Answer Checked By - Robin (JavaFixing Admin)