本文介绍了CronTrigger没有触发在指定的时间,而是发送多个推送通知,在错误的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用亚马逊推送通知在我的Spring MVC 4项目。我已经使用CronTrigger发送推送通知的Andr​​oid应用程序每天早上8点。我还使用时区连同CronTrigger以便用户根据其各自的时区得到通知。

I am using Amazon Push Notifications in my Spring MVC 4 project. I have used CronTrigger to send push notifications to Android app everyday at 8am. I have also used Timezone along with CronTrigger so that users get notifications according to their respective timezones.

下面是我的 WebConfig.java:

@Configuration
@EnableScheduling
@EnableWebMvc
@ComponentScan(basePackages="com.project")
public class WebConfig implements SchedulingConfigurer
{
    protected static final Logger slf4jLogger  =  Logger.getLogger(WebConfig.class.getName());
private static final String cronExpression = "0 8 * * * ?";



/*@Bean
public MobileNotifSchedulerBean schedulerbean()
{
    return new MobileNotifSchedulerBean();
}*/

@Bean
public InternalResourceViewResolver getInternalResourceViewResolver()
{
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    resolver.setSuffix(".html");
    resolver.setSuffix(".htm");
    return resolver;
}

@Bean
CronTrigger cronTrigger()
{
  //The code in FetchUserTimeZones.java fetches all the user timezones which are stored in DynamoDb. Eg timeZone = "Asia/Calcutta";
   String timeZone = null;
    HashSet<String> userTimeZonesfromDB = FetchUserTimeZones.fetchUserTimeZone();
    for (String s : userTimeZonesfromDB)
    {
        timeZone = s;
        slf4jLogger.info(s);
    }
    return new CronTrigger(cronExpression, TimeZone.getTimeZone(timeZone));
}


@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar)
{

    taskRegistrar.addCronTask(new CronTask(new MobileNotifSchedulerBean(), cronTrigger()));
}

@Bean(destroyMethod="shutdown")
public Executor taskExecutor()
{
    return Executors.newScheduledThreadPool(1);
  }
}

下面是我的 MobileNotifSchedulerBean:这code从DynamoDb获取随机的问题,他们推送通知的CronTrigger设定时间(上午8点)的帮助下发出的用于每个GCM registrationID。我已经使用的 snsmobilepush.zip 从的。

Here is my MobileNotifSchedulerBean:This code fetches a random question from DynamoDb and send them in Push Notification for each GCM registrationID with help of the CronTrigger set to time (8am). I have used snsmobilepush.zip from http://docs.aws.amazon.com/sns/latest/dg/mobile-push-gcm.html.

@EnableScheduling
public class MobileNotifSchedulerBean implements Runnable
{
   protected static final Logger slf4jLogger  = Logger.getLogger(MobileNotifSchedulerBean.class.getName());

public MobileNotifSchedulerBean()
{
     run();
}

public void sendQuestionNotif()
{
    try
    {
        HashSet<String> reg_ids = FetchRegistrationIDs.fetchItems();
        for (String s : reg_ids)
        {
            String REGISTRATION_IDs = s;
            slf4jLogger.info(s);
            MobileSNSPushNotification.sendNotification(REGISTRATION_IDs);
        }
    }
    catch (IOException e)
    {
        //e.printStackTrace();
        slf4jLogger.error(e);
        slf4jLogger.error(e.getMessage());
        slf4jLogger.error(e.getStackTrace());
    }
}


@Override
public void run()
{
    sendQuestionNotif();
}
}

请帮我,我也出了问题。唯一的问题是推送通知要去要去只有1%的用户推送通知,每天(上午8时),在错误的时间,也多数量的通知代替。 TIA。

Please help me where I have went wrong. Only problem is push notifications are going in wrong times and also multiple number of notifications instead of going only 1 push notification per user, per day(8am). TIA.

更新:有cron中前pression修正。我纠正它。

Update: There was a correction in Cron Expression. I corrected it.

private static final String cronExpression = "0 0 8 * * ?";  // For everyday 8 am.

但还是问题一直未动

But still problem hasn't fixed

推荐答案

您应该创建一个单独的时间表为每个用户

You should create a separate schedule for each user

// spring bean
class Task implements Runnable {

    public Task(TaskScheduler scheduler, String cron) {
        HashSet<String> userTimeZonesfromDB = FetchUserTimeZones.fetchUserTimeZone();
        for (String tz : userTimeZonesfromDB) {
            scheduler.schedule(this, new CronTrigger(cron, TimeZone.getTimeZone(tz)));
        }
    }

    @Override
    public void run() {
        // notify
    }
}

这篇关于CronTrigger没有触发在指定的时间,而是发送多个推送通知,在错误的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 12:59