Trigger email using java after 2 hours [duplicate] - java

This question already has answers here:
Scheduling a time in the future to send an email in Java or Python
(6 answers)
Closed 5 years ago.
I want to trigger an email with user details in that email after two hours if the user updated basic information. I used TimerTask class in java and it was working fine. But planning to use ScheduledThreadPoolExecutor.java.
My question is,
Since using multithreading, I must avoid race condition (For that I am using ScheduledThreadPoolExecutor.java or TimerTask.java).
User can update his/her basic details N number of times within two hours.So, after two hours I have to send mail with updated details.How to track that timing?
Is there any better approach for this?
Since using multithreading, What are the scenarios I have to cover?
Thanks in advance,

A quick approach is, each time when you do such update email, capture all the necessary data (i.e. copy it) within the task so you totally avoid locking concern. Given your application I think this is sensible and sufficient.
For 2) depends on if you wish to do conflation (i.e. keep only the latest task in the queue) or you wishes to just let me be.

Related

Saving permanently user input for later use and expanding your program [duplicate]

This question already has answers here:
Best way to save data in a Java application?
(4 answers)
Closed 2 years ago.
EDITED QUESTION
I made a small sequential program in Java which can calculate my daily bonus for my job. It's not the most beautiful or optimized bit of code but hey it works!
Now it would be cool to have this actually save my input for that day somehow and even when I terminate the program the next time I open it should still have stored that end value somewhere. Maybe even go back to that specific day and check my PPH (productive hours) and maybe even calculate the whole month (which is actually the end goal and track my performance).
Any suggestions?
***** Answers I found ******
This is the first one i found but is not super useful if you would like that your colleagues also use your application. For single use and store a file locally it would work. https://attacomsian.com/blog/gson-read-write-json#
Using an SQL data base: https://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html

Restrict the If block [duplicate]

This question already has answers here:
Run a piece of code only once when an application is installed [duplicate]
(4 answers)
Closed 5 years ago.
I have an if condition in my Java code in Android and I want to enter the block it's the first time the user has run the app. But for the second time and thereafter, they ignore the conditions.
How can I restrict the if block in my Java code?
This is my if block condition:
if(AppSharedPrefs.getInstance(SplashActivity.this)
.readPrefs‌​(SplashActivity.this‌​, "language").isEmpty())
{
AppSharedPrefs.getInstance(SplashActivity.this)
.writePrefs(S‌​plashActivity.this, "language","English");
}
You need to have shared preference for your purpose. On first time store value true to shared preference and when next time user comes to the app get value from shared preference if that is true you dont need to call your code
"Simple": you pick one of the options for storing data on the device.
And then, your code reads that data back every time the application starts.
See here for an overview of options, and there for the option you should pick (shared preferences).
In other words: the whole idea is that you have to enable yourself to store data; so that your code can later make decisions based on that data. That is all there is to this.

How does Command Pattern enables Undo? [duplicate]

This question already has answers here:
Best design pattern for "undo" feature [duplicate]
(3 answers)
Closed 6 years ago.
Can someone help me understand how can the Command Pattern in Java or C# enables "undo" functionality in application design? Any explanation would be appreciated.
Undo functionality requires that you store the last n-number of user actions. Typically a user action simply results in a function call, so its very hard to store it. Its even harder to store it in a way that is undoable.
By abstracting a user action into an object, you can easily put a set of these objects into a data structure (typically Stack) and if you put an Undo method onto the object you just go through the structure and call the Undo method as the user requests the undo operation.
Since Command is already set up to encapsulate an operation (user or otherwise) it is an excellent starting point for building undo functionality.

How to implement keystroke logging by java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you record keystrokes when operating on another window in Java?
Try to get all keystroke on the OS. any ideas?
Yeah possibly u should get the keystrokes of the users through the use of currentTimeMillis method or so in java in any of the keyPressed() or keyReleased() methods in java.Then save those time recordings in any variable get the time difference and then compare it with the keystrokes stored in database with a particular threshold.It should work then.During the recording phase make the user type the intended filed 5-6 times, take the keystrokes recording of the user and then save its mean in the database and compare it with the current keystrokes of the user while he tries to log in.

Convert Large Switch to Hash (Or Other Recommendation)

I currently have an Android application that displays a schedule for a ferry boat. The application can display the full schedule (just a giant list), but the selling point in the application is it will display when the next two ferries are departing and how long from the current time that departure is.
I am relatively new to Java and currently use large Switch() statements in my code. Basically it gets the current phone time and compares it to all of the times in the schedule at which point it displays the next two departure times and then calculates the difference between current time and the departure times.
I am sure that a switch statement is not the best idea for speed purposes as well as code changing purposes. For example if one time changes its a bunch of lines of code to go in and fix for that one time change. Also if the entire schedule changes everyone has to update their app for the time change to take effect. My ideal situation would be to store a file somewhere on my webserver that could be downloaded and inserted into a hashmap (I think is the correct term) that would load the new schedule if there was a time change.
Not sure how confusing this is, but it would be greatly appreciated if someone could explain how I might use a hashmap or something else you might recommend to get this task accomplished. Currently the variables are the two ferry terminals as well as the day of the week since the schedule changes per day (monday, tues-friday, saturday, sunday).
Below is a screenshot of the application so you can understand it if my post wasn't clear. Thank you in advance.
Screenshot:
Store the schedule objects in a sorted array. You can then binary search the array for the first value greater than the current time. You'll probably use some parent array consisting of the location and applicable day of the week.
You can easily write that kind of data structure to a file that is read & parsed by the application for updates instead of being compiled into the code.
Details of this? First, understand resources in Android. If no updated schedule exists, fall back to the default resource.
Second, use an HTTP head request to check if a newer file exists. If it does, parse, download & save state. Saving Android Activity state using Save Instance State.
Finally, XML is handy for data distribution, even if it's not fast. Everybody understands it and it's easy to update or hand off.
<ferry location=0 time=2045>
<day>1</day>
<day>2</day>
<day>3</day>
<day>4</day>
<day>5</day>
</ferry>
<ferry location=0 time=0800>
<day>6</day>
</ferry>
You will need something like a database to hold the schedule data. That will help you to seperate code from data. I'm not familiar with Android but i think there is a interface to sqlite database on the device.
Further, as this is an application on a small device you may connect to the schedule database on a server thru the internet connection. That way you have to maintain schedule data only in one place (on the server) and clients will use always up to date data.

Categories

Resources