Schedule next run time is incorrect - java

I have a simple Schedule like below which should run every 30 minutes from 6pm to 10:30pm.
import javax.ejb.Schedule;
import javax.ejb.Schedules;
import javax.ejb.Singleton;
#Singleton
public class XxxJob {
#Schedules({
#Schedule(minute = "0,30", hour = "18-22", dayOfWeek = "Mon-Fri",
timezone = "Europe/London", persistent = false)
})
public void process() {
//...
}
}
But when I check the details of the timer on the Wildfly console, I see this:
{"time-remaining" => 30039141L,"next-timeout" => 1498757400000L,"calendar-timer" => true,"persistent" => false,"schedule" => {"year" => "*","month" => "*","day-of-month" => "*","day-of-week" => "Mon-Fri","hour" => "18-22","minute" => "0,30","second" => "0","timezone" => "Europe/London","start" => undefined,"end" => undefined}}
It says the next run (next-timeout) is at 1498757400000L (epoch millis) but that corresponds to 2017-06-29T18:30+01:00[Europe/London] and not 18:00.
Why is the timer scheduled to run at 18:30 and not at 18:00?
Note 1: if I amend the schedule to only run at 18:00 it works as expected:
#Schedule(minute = "0", hour = "18", dayOfWeek = "Mon-Fri",
timezone = "Europe/London", persistent = false)
Note 2: if I remove the #Schedules annotation it works as expected too.

Related

How to schedule a task which checks for an Email every 15 min. If mail is found, then task should end and only execute the next day using Java

public static void main(String[] args) throws Exception {
String saveDirectorySib = PropertiesHolder.getInstance().getProperty(PropertiesHolder.SIB_Txt_FOLDER_KEY);
JobDetail Job = JobBuilder.newJob(SibCronScheduler.class).build();
Trigger t1 = TriggerBuilder.newTrigger().withIdentity("CronTrigger")
.withSchedule(CronScheduleBuilder.cronSchedule(PropertiesHolder.getInstance().getProperty(PropertiesHolder.schedulerExec))).build();
Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
sched.start();
sched.scheduleJob(Job, t1);
EmailFoundBoolean mailFetch = new EmailFoundBoolean();
boolean mailFound = mailFetch.emailFoundValueReturn();
System.out.println("Value is"+mailFound);
if(mailFound== true)
{ System.out.println(mailFound);
// sched.deleteJob(Job.getKey());
// sched.pauseJob(Job.getKey());
sched.standby();
System.out.println("Process completed for the day.");
}
else
{
System.out.println("Process continues");
}
public class SibCronScheduler implements Job{
#Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// Timer time = new Timer();
// ScheduledTask st = new ScheduledTask();
// time.schedule(st, 9000,1000 * 60 * 15);
System.out.println("Checking for the mail from South Indian Bank....");
String saveDirectorySib = PropertiesHolder.getInstance().getProperty(PropertiesHolder.SIB_Txt_FOLDER_KEY);
mailRead.setSaveDirectory1(saveDirectorySib);
try {
mailRead.sibEmailReader();
} catch (Exception e) {
e.printStackTrace();
}
}
}
I tried Java scheduler and quartz scheduler. Using java scheduler the code runs every 15 min. But still it does not work. I need to run the process every day from 6 pm to 12 am. every 15 min. And if the mail is found. stop the scheduler for the day and restart the process the next day at 6 pm. Syste.exit(0) stops the code but I need to restart it the next day.
You said:
I need to run the process every day from 6 pm to 12 am. every 15 min. And if the mail is found. stop the scheduler for the day and restart the process the next day at 6 pm.
You could simplify your problem by scheduling a task to run every 15 minutes around the clock. Let that task check (a) whether the daily chore was performed, and (b) the time-of-day and date.
The idea here is to let you task run needlessly, but ever so briefly, around the clock. Checking the current moment, and comparing to when the chore was last performed, take a minuscule amount of CPU time and memory. So there really is no point to bothering with suspending the checks for part of each day.
public class EveningEmailCheckingTask implements Runnable
{
final private ZoneId zoneId;
final private LocalTime startTime ;
private ZonedDateTime whenEmailRetrieved;
public EveningEmailCheckingTask( final ZoneId z , final LocalTime lt )
{
this.zoneId = Objects.requireNonNull( z ) ;
this.startTime = Objects.requireNonNull( lt ) ;
this.whenEmailRetrieved = ZonedDateTime.now( this.zoneId ).minusDays( 1 ) ; // Initialize a value so our main code can skip null check.
}
#Override
public void run()
{
ZonedDateTime now = ZonedDateTime.now( this.zoneId ) ;
// See if chore has been completed successfully today.
LocalDate then = this.whenEmailRetrieved.toLocalDate() ;
LocalDate today = now.toLocalDate() ;
if( then.isBefore( today ) )
{
if( now.toLocalTime().isBefore( this.startTime ) ) { return ; }
// … Perform chore.
// … If email received, update `this.whenEmailRetrieved`.
}
else if( then.isEqual( today ) )
{
return ; // Daily chore already performed.
}
else if( then.isAfter( today ) )
{
// Houston, we have a problem. The task was performed in the future.
}
else
{
// Should never reach this point. Defensive check in case our if-tests above are flawed.
}
}
}
Instantiate.
EveningEmailCheckingTask task = new EveningEmailCheckingTask( ZoneId.of( "Asia/Kolkata" , LocalTime.of( 18 , 0 ) ) ;
Create a ScheduledExecutorService, and remember it. You’ll need to shutdown that service before your app exits, otherwise the backing thread pool can continue indefinitely like a zombie 🧟‍♂️.
ScheduledExecutorService ses = Executors. … ;
Schedule your task.
ses.scheduleAtFixedRate( … ) ;

JavaFX MediaPlayer: MP4 loop freeze, sound distorted, Windows 7

We have a problem with JavaFX MediaPlayer for more than a month now. We read and tried all the similar problems here, nothing worked.
Simplest example: It's an info app with 2 videos from a folder played continualy in a loop. After some time (sometimes 2h, sometimes 2 days)
videos start to slow down, duration is longer, occasional freeze, sound becomes distorted.
OS: win7 64b (Ultimate) SP1
Intel Celeron CPU N3150 #1.6GHz
8 GB Ram
java 1.8 and 1.10, 64b
Tried:
OS update, latest.
Drivers updates, other drivers.
JDK 64b, 1.8 latest updates, JDK 1.10 latest.
Changing code (adding dispose, closing, setting null, caching... every suggestion on stackoverflow)
Changing video encoders: MPEG-4, H.264 and H.264-HD, 1500/2000/4000/8000 kbps, 25fps
Changing resolutions: 1280x720,
Sound: AAC, 1 and 2 channels, 48 & 44 kHz, 128kbps bit rate and others. Tried videos without audio.
Almost every possible combination of these video/sound encoders, bitrates, resolutions, framerates ...
We profiled for memory leaks: memory grows for some time and it stops growing. It's ok (we think).
Nothing we tried has worked. No errors, no exceptions on MediaView, Media, Player. No system events.
Confusing: we noticed that that in normal operations CPU is 20-30%, but, when videos start to freeze, it's lower, 5-15%.
Code (latest version):
import java.io.File
import akka.actor.{Actor, ActorLogging, Props, Timers}
import akka.pattern._
import com.commercial.activity.MMDriver.ShowNextMedia
import javafx.event.EventHandler
import javafx.scene.media.{Media, MediaErrorEvent, MediaPlayer, MediaView}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
object MMDriver {
def props(rootFolder: File, allowedExtension: List[String],
showMedia: (MMedia) => Unit): Props = Props(new MMDriver(rootFolder, allowedExtension, showMedia))
case object Start
case class MediaCreated(list: List[MMedia])
case object ShowNextMedia
}
class MMDriver(rootFolder: File, allowedExtensions: List[String],
showMedia: (MMedia) => Unit) extends Actor with ActorLogging with Timers {
object TimerKey
var started = false
var medias = List[MMedia]()
var index = 0
override def receive: Receive = {
case MMDriver.Start if (!started) =>
started = true
Future {
loadMedia(rootFolder, allowedExtensions)
.map { file =>
val media = new Media(file.toURI.toURL.toString)
val player = new MediaPlayer(media)
//next video
player.setOnEndOfMedia(new Runnable {
override def run(): Unit = {
self ! ShowNextMedia
}
})
val mediaView = new MediaView(player)
setErrorHandlers(player, media, mediaView)
MMedia(file, media, player, mediaView)
}
}
.map(media => MMDriver.MediaCreated(media.toList))
.pipeTo(self)
case MMDriver.MediaCreated(list) =>
medias = list
if (medias.nonEmpty) {
self ! ShowNextMedia
}
case MMDriver.ShowNextMedia =>
val media = medias(index)
showMedia(media)
index = index + 1
index = index % medias.size
case msg =>
log.info("not processed message {}", msg)
}
private def loadMedia(folder: File, allowedExtensions: List[String]): Array[File] = {
folder.listFiles()
.filter { file =>
allowedExtensions.exists(file.getName.endsWith(_))
}
}
private def setErrorHandlers(player: MediaPlayer, media: Media, mediaView: MediaView): Unit = {
player.setOnError(new Runnable {
override def run(): Unit = {
log.error(player.getError, "error in player")
}
})
media.setOnError(new Runnable {
override def run(): Unit = {
log.error(media.getError, "error in media")
}
})
mediaView.setOnError(new EventHandler[MediaErrorEvent] {
override def handle(event: MediaErrorEvent): Unit = {
log.error(event.getMediaError, "error in mediaView")
}
})
}
}
Any suggestion what to try next - we are out of ideas?!

localized duration format for french

What is the Python analog of Time4J's code example:
// duration in seconds normalized to hours, minutes and seconds
Duration<?> dur = Duration.of(337540, ClockUnit.SECONDS).with(Duration.STD_CLOCK_PERIOD);
// custom duration format => hh:mm:ss
String s1 = Duration.Formatter.ofPattern("hh:mm:ss").format(dur);
System.out.println(s1); // output: 93:45:40
// localized duration format for french
String s2 = PrettyTime.of(Locale.FRANCE).print(dur, TextWidth.WIDE);
System.out.println(s2); // output: 93 heures, 45 minutes et 40 secondes
It is easy to get 93:45:40:
#!/usr/bin/env python3
from datetime import timedelta
dur = timedelta(seconds=337540)
print(dur) # -> 3 days, 21:45:40
fields = {}
fields['hours'], seconds = divmod(dur // timedelta(seconds=1), 3600)
fields['minutes'], fields['seconds'] = divmod(seconds, 60)
print("%(hours)02d:%(minutes)02d:%(seconds)02d" % fields) # -> 93:45:40
but how do I emulate PrettyTime.of(Locale.FRANCE).print(dur, TextWidth.WIDE) Java code in Python (without hardcoding the units)?
babel module allows to get close to desired output:
from babel.dates import format_timedelta # $ pip install babel
print(", ".join(format_timedelta(timedelta(**{unit: fields[unit]}),
granularity=unit.rstrip('s'),
threshold=fields[unit] + 1,
locale='fr')
for unit in "hours minutes seconds".split()))
# -> 93 heures, 45 minutes, 40 secondes
It handles locale and plural forms automatically e.g., for dur = timedelta(seconds=1) it produces:
0 heure, 0 minute, 1 seconde
Perhaps a better solution would be to translate the format string manually using standard tools such as gettext.
If you're using Kotlin, I just came across a similar problem with the Kotlin Duration type with localized formatting and because I couldn't find a good solution, I wrote one myself. It is based on APIs provided starting in Android 9 (for localized units), but with a fallback to English units for lower Android versions so it can be used with lower targeting apps.
Here's how it looks like on the usage side (see Kotlin Duration type to understand 1st line):
val duration = 5.days.plus(3.hours).plus(2.minutes).plus(214.milliseconds)
DurationFormat().format(duration) // "5day 3hour 2min"
DurationFormat(Locale.GERMANY).format(duration) // "5T 3Std. 2Min."
DurationFormat(Locale.forLanguageTag("ar").format(duration) // "٥يوم ٣ساعة ٢د"
DurationFormat().format(duration, smallestUnit = DurationFormat.Unit.HOUR) // "5day 3hour"
DurationFormat().format(15.minutes) // "15min"
DurationFormat().format(0.hours) // "0sec"
As you can see, you can specify a custom locale to the DurationFormat type. By default it uses Locale.getDefault(). Languages that have different symbols for number than romanic are also supported (via NumberFormat). Also, you can specify a custom smallestUnit, by default it is set to SECOND, so milliseconds will not be shown. Note that any unit with a value of 0 will be ignored and if the entire number is 0, the smallest unit will be used with the value 0.
This is the full DurationFormat type, feel free to copy (also available as a GitHub gist incl. unit tests):
import android.icu.text.MeasureFormat
import android.icu.text.NumberFormat
import android.icu.util.MeasureUnit
import android.os.Build
import java.util.Locale
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.days
import kotlin.time.hours
import kotlin.time.milliseconds
import kotlin.time.minutes
import kotlin.time.seconds
#ExperimentalTime
data class DurationFormat(val locale: Locale = Locale.getDefault()) {
enum class Unit {
DAY, HOUR, MINUTE, SECOND, MILLISECOND
}
fun format(duration: kotlin.time.Duration, smallestUnit: Unit = Unit.SECOND): String {
var formattedStringComponents = mutableListOf<String>()
var remainder = duration
for (unit in Unit.values()) {
val component = calculateComponent(unit, remainder)
remainder = when (unit) {
Unit.DAY -> remainder - component.days
Unit.HOUR -> remainder - component.hours
Unit.MINUTE -> remainder - component.minutes
Unit.SECOND -> remainder - component.seconds
Unit.MILLISECOND -> remainder - component.milliseconds
}
val unitDisplayName = unitDisplayName(unit)
if (component > 0) {
val formattedComponent = NumberFormat.getInstance(locale).format(component)
formattedStringComponents.add("$formattedComponent$unitDisplayName")
}
if (unit == smallestUnit) {
val formattedZero = NumberFormat.getInstance(locale).format(0)
if (formattedStringComponents.isEmpty()) formattedStringComponents.add("$formattedZero$unitDisplayName")
break
}
}
return formattedStringComponents.joinToString(" ")
}
private fun calculateComponent(unit: Unit, remainder: Duration) = when (unit) {
Unit.DAY -> remainder.inDays.toLong()
Unit.HOUR -> remainder.inHours.toLong()
Unit.MINUTE -> remainder.inMinutes.toLong()
Unit.SECOND -> remainder.inSeconds.toLong()
Unit.MILLISECOND -> remainder.inMilliseconds.toLong()
}
private fun unitDisplayName(unit: Unit) = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val measureFormat = MeasureFormat.getInstance(locale, MeasureFormat.FormatWidth.NARROW)
when (unit) {
DurationFormat.Unit.DAY -> measureFormat.getUnitDisplayName(MeasureUnit.DAY)
DurationFormat.Unit.HOUR -> measureFormat.getUnitDisplayName(MeasureUnit.HOUR)
DurationFormat.Unit.MINUTE -> measureFormat.getUnitDisplayName(MeasureUnit.MINUTE)
DurationFormat.Unit.SECOND -> measureFormat.getUnitDisplayName(MeasureUnit.SECOND)
DurationFormat.Unit.MILLISECOND -> measureFormat.getUnitDisplayName(MeasureUnit.MILLISECOND)
}
} else {
when (unit) {
Unit.DAY -> "day"
Unit.HOUR -> "hour"
Unit.MINUTE -> "min"
Unit.SECOND -> "sec"
Unit.MILLISECOND -> "msec"
}
}
}
This humanize package may help. It has a french localization, or you can add your own. For python 2.7 and 3.3.
Using pendulum module:
>>> import pendulum
>>> it = pendulum.interval(seconds=337540)
>>> it.in_words(locale='fr_FR')
'3 jours 21 heures 45 minutes 40 secondes'

Nullpointer Exception in Unit Test

I'm working on a Groovy Grails application and specifically at the moment I'm working on a service that will allow me to generate PLSQL. I'm having some issues however with working out how to solve this Null pointer exception with my unit test below.
Here is the error message:
java.lang.NullPointerException
at com.bt.mlp.TemplatingService.$tt__buildTemplateStepSql(TemplatingService.groovy:59) at com.bt.mlp.TemplatingServiceSpec.Can create PLSQL for TemplateStep(TemplatingServiceSpec.groovy:113)
If I click on the first NullPointer it takes me to def result = in the second block of code.
If I click on the second NullPointer it takes me to def result = service.buildTemplateStepSql(templateStep) in the unit test.
Unit Test:
void "Can create PLSQL for TemplateStep" () {
setup:
createTemplateStep()
def validString =
"""
select sysid into v_mt_sysid from template where name = 'Add FASTE 60 Card';
MLP_TEMPLATE_CP.TEMPLATE_STEPS_INS(IN_MT_SYSID => v_mt_sysid
,IN_STEP => 'step'
,IN_VALIDATION_PROCEDURE => 'Procedure'
,IN_ACTION_PROCEDURE => 'MLP_CARD_INFILL'
,IN_TRANSACTION_TABLE => 'Table'
,IN_DISPLAY_LABLE => 'Capture plan inputs'
,IN_WINDOW_LABLE => 'Capture plan inputs'
,IN_CANCEL_PROCEDURE => 'Cancel Procedure'
,IN_END_PLAN_PHASE => 'Designed'
,IN_MFOUT_FLAG => 'N'
,IN_STEP_SEQ => 1
,IN_SWF_NAME => 'Name'
,OUT_TS_SYSID => v_ts_sysid );
"""
when:
def templateStep = TemplateStep.findByStep("step")
def result = service.buildTemplateStepSql(templateStep)
then:
result == validString
}
Code being tested:
def buildTemplateStepSql(TemplateStep templateStep) {
println templateStep
def result =
"""
select sysid into v_mt_sysid from template where name = 'Add FASTE 60 Card';
MLP_TEMPLATE_CP.TEMPLATE_STEPS_INS(IN_MT_SYSID => v_mt_sysid
,IN_STEP => '${templateStep.step}'
,IN_VALIDATION_PROCEDURE => '${templateStep.validationProcedure}'
,IN_ACTION_PROCEDURE => '${templateStep.actionProcedure}'
,IN_TRANSACTION_TABLE => '${templateStep.transactionTable}'
,IN_DISPLAY_LABLE => '${templateStep.displayLable}'
,IN_WINDOW_LABLE => '${templateStep.windowLable}'
,IN_CANCEL_PROCEDURE => '${templateStep.cancelProcedure}'
,IN_END_PLAN_PHASE => '${templateStep.endPlanPhase}'
,IN_MFOUT_FLAG => '${templateStep.mfOutFlag}'
,IN_STEP_SEQ => '${templateStep.stepSeq}'
,IN_SWF_NAME => '${templateStep.swfName}'
,OUT_TS_SYSID => v_ts_sysid );
"""
return result
}
Any help would be greatly appreciated.

Processing CQEngine ResultSet with Scala foreach is very slow

I'm trying to process CQEngine's ResultSet using Scala's foreach, but the result is very slow.
Following is the snippet of what I'm trying to do
import collection.JavaConversions._
val query = existIn(myOtherCollection, REFERENCE, REFERENCE)
val resultSet = myIndexCollection.retrieve(query)
resultSet.foreach(r =>{
//do something here
})
Somehow the .foreach method is very slow. I tried to debug by putting SimonMonitor and change the .foreach using while(resultSet.hasNext), surprisingly, every call to hasNext method takes about 1-2 seconds. That's very slow.
I tried to create the same version using Java, and the Java version is super fast.
Please help
I am not able to reproduce your problem with the below test code. Can you try it on your system and let me know how it runs?
(Uncomment line 38, garages.addIndex(HashIndex.onAttribute(Garage.BRANDS_SERVICED)), to make BOTH the Scala and Java iterators run blazingly fast...)
The output first (time in milliseconds):
Done adding data
Done adding index
============== Scala ==============
Car{carId=4, name='BMW M3', description='2013 model', features=[radio, convertible]}
Time : 3 seconds
Car{carId=1, name='Ford Focus', description='great condition, low mileage', features=[spare tyre, sunroof]}
Time : 1 seconds
Car{carId=2, name='Ford Taurus', description='dirty and unreliable, flat tyre', features=[spare tyre, radio]}
Time : 2 seconds
============== Java ==============
Car{carId=4, name='BMW M3', description='2013 model', features=[radio, convertible]}
Time : 3 seconds
Car{carId=1, name='Ford Focus', description='great condition, low mileage', features=[spare tyre, sunroof]}
Time : 1 seconds
Car{carId=2, name='Ford Taurus', description='dirty and unreliable, flat tyre', features=[spare tyre, radio]}
Time : 2 seconds
Code below:
import collection.JavaConversions._
import com.googlecode.cqengine.query.QueryFactory._
import com.googlecode.cqengine.CQEngine;
import com.googlecode.cqengine.index.hash._;
import com.googlecode.cqengine.IndexedCollection;
import com.googlecode.cqengine.query.Query;
import java.util.Arrays.asList;
object CQTest {
def main(args: Array[String]) {
val cars: IndexedCollection[Car] = CQEngine.newInstance();
cars.add(new Car(1, "Ford Focus", "great condition, low mileage", asList("spare tyre", "sunroof")));
cars.add(new Car(2, "Ford Taurus", "dirty and unreliable, flat tyre", asList("spare tyre", "radio")));
cars.add(new Car(3, "Honda Civic", "has a flat tyre and high mileage", asList("radio")));
cars.add(new Car(4, "BMW M3", "2013 model", asList("radio", "convertible")));
// add cruft to try and slow down CQE
for (i <- 1 to 10000) {
cars.add(new Car(i, "BMW2014_" + i, "2014 model", asList("radio", "convertible")))
}
// Create an indexed collection of garages...
val garages: IndexedCollection[Garage] = CQEngine.newInstance();
garages.add(new Garage(1, "Joe's garage", "London", asList("Ford Focus", "Honda Civic")));
garages.add(new Garage(2, "Jane's garage", "Dublin", asList("BMW M3")));
garages.add(new Garage(3, "John's garage", "Dublin", asList("Ford Focus", "Ford Taurus")));
garages.add(new Garage(4, "Jill's garage", "Dublin", asList("Ford Focus")));
// add cruft to try and slow down CQE
for (i <- 1 to 10000) {
garages.add(new Garage(i, "Jill's garage", "Dublin", asList("DONT_MATCH_CARS_BMW2014_" + i)))
}
println("Done adding data")
// cars.addIndex(HashIndex.onAttribute(Car.NAME));
// garages.addIndex(HashIndex.onAttribute(Garage.BRANDS_SERVICED));
println("Done adding index")
val query = existsIn(garages, Car.NAME, Garage.BRANDS_SERVICED, equal(Garage.LOCATION, "Dublin"))
val resultSet = cars.retrieve(query)
var previous = System.currentTimeMillis()
println("============== Scala ============== ")
// Scala version
resultSet.foreach(r => {
println(r);
val t = (System.currentTimeMillis() - previous)
System.out.println("Time : " + t / 1000 + " seconds")
previous = System.currentTimeMillis()
})
println("============== Java ============== ")
previous = System.currentTimeMillis()
// Java version
val i: java.util.Iterator[Car] = resultSet.iterator()
while (i.hasNext) {
val r = i.next()
println(r);
val t = (System.currentTimeMillis() - previous)
System.out.println("Time : " + t / 1000 + " seconds")
previous = System.currentTimeMillis()
}
}
}

Categories

Resources