最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

scheduled tasks - How to play Azan in Java by TimerTask - Stack Overflow

programmeradmin1浏览0评论

I am developing a Java application that I want to play the Azan file at the five Islamic religious times, which are different for each day and are calculated according to astronomical calculations for each day. For this, it is necessary for the running program to continuously check what day and time it is, and for example, if the Azan time is 12:15 today, the Azan file should be played, and so on for other times and days.

I tried to use the Java Task Timer class for this purpose, but I have trouble running the task at the desired times and the program does not run properly. I have given the sample program code below.

package org.example;

import java.time.LocalTime;
import java.util.Timer;
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
    
    @Override
    public void run() { 
//CACLULATE THE TIMES FUNCTION();

        LocalTime now = LocalTime.now();
        LocalTime morning = LocalTime.parse( "5:32" );
        LocalTime noon = LocalTime.parse( "12:15" );
        LocalTime afternoon = LocalTime.parse( "15:18" );
        LocalTime night = LocalTime.parse( "17:59" );
            switch(now){
                case morning:
                    System.out.println( "good morning!" );
                    hit = true;
                    break;
                case noon:
                    System.out.println( "good noon!" );
                    hit=true;
                    break;
                case afternoon:
                    System.out.println( "good afternoon!" );
                    hit = true;
                    break;
                case nightT:
                    System.out.println( "good night!" );
                    hit = true;
                    break;
                default:
                    System.out.println( "BAD TIME!" );
            }
        }
    }
class TTest{
    public static void main(String args[]){
        MyTimerTask task = new MyTimerTask();
        Timer timer = new Timer();
        timer.schedule(task, 1000);
        try{
            Thread.sleep(5000);
        }catch (InterruptedException e){}
    }
}

I am developing a Java application that I want to play the Azan file at the five Islamic religious times, which are different for each day and are calculated according to astronomical calculations for each day. For this, it is necessary for the running program to continuously check what day and time it is, and for example, if the Azan time is 12:15 today, the Azan file should be played, and so on for other times and days.

I tried to use the Java Task Timer class for this purpose, but I have trouble running the task at the desired times and the program does not run properly. I have given the sample program code below.

package org.example;

import java.time.LocalTime;
import java.util.Timer;
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
    
    @Override
    public void run() { 
//CACLULATE THE TIMES FUNCTION();

        LocalTime now = LocalTime.now();
        LocalTime morning = LocalTime.parse( "5:32" );
        LocalTime noon = LocalTime.parse( "12:15" );
        LocalTime afternoon = LocalTime.parse( "15:18" );
        LocalTime night = LocalTime.parse( "17:59" );
            switch(now){
                case morning:
                    System.out.println( "good morning!" );
                    hit = true;
                    break;
                case noon:
                    System.out.println( "good noon!" );
                    hit=true;
                    break;
                case afternoon:
                    System.out.println( "good afternoon!" );
                    hit = true;
                    break;
                case nightT:
                    System.out.println( "good night!" );
                    hit = true;
                    break;
                default:
                    System.out.println( "BAD TIME!" );
            }
        }
    }
class TTest{
    public static void main(String args[]){
        MyTimerTask task = new MyTimerTask();
        Timer timer = new Timer();
        timer.schedule(task, 1000);
        try{
            Thread.sleep(5000);
        }catch (InterruptedException e){}
    }
}
Share Improve this question edited Feb 6 at 10:51 Mark Rotteveel 109k226 gold badges155 silver badges219 bronze badges asked Feb 6 at 6:55 mhm.mapmhm.map 1511 silver badge10 bronze badges 9
  • 1 how about doing it in a batch and set the scheduling accordingly? Not sure if you can get it done in a single scheduling, though. Do the times change from day to day, or can they be hardcoded? Also, don't ingore exceptions, how are you going to know your code causes errors? – Stultuske Commented Feb 6 at 6:58
  • Times are different for each day.I don't know what your method is like. – mhm.map Commented Feb 6 at 7:04
  • 2 Can LocalTime really be used in a switch statement like that? Even for pattern matching it should match on the type of class, not how you are using it currently. I rather ditch the switch statement and find the Duration between now and the different LocalTime and trigger hit if the duration is between a certain acceptance range i.e. 5 seconds – pebble unit Commented Feb 6 at 7:05
  • Yeah, double confirming it on my IDE shows Incompatible types. Found: 'java.time.LocalTime', required: 'char, byte, short, int, Character, Byte, Short, Integer, String, or an enum' – pebble unit Commented Feb 6 at 7:08
  • I think this is one of the important issues with this program. How can I remove the switch? What is your suggestion? – mhm.map Commented Feb 6 at 7:09
 |  Show 4 more comments

2 Answers 2

Reset to default 4

Avoid Timer

The Timer and TimerTask classes are legacy. They were supplanted by the Executors framework added to Java 5. So noted in the Javadoc.

ScheduledExecutorService

Define your task, playing the Azan file, as an implementation of Runnable.

Instantiate a ScheduledExecutorService. Keep around for repeated use. Be sure to shut it down gracefully before your app exits, otherwise its backing thread pool may continue running indefinitely like a zombie

发布评论

评论列表(0)

  1. 暂无评论