I want to continuously check a table in the database to see whether a new row has been added to it. This runs as a back ground process. I think a thread should be used for this task. but I have no idea how to write the code. Can somebody help me with this please?
Well, you're not really giving us much to go on here.
You might find it easier to use a database trigger, which will fire some code whenever a specified action occurs (e.g. insertion of new data). You will need to look up details for your specific database.
I just realised that you have probably already tried to use triggers and failed: sql trigger not work as expected. Either approach will work, but I would prefer keeping everything in the DB and avoiding external processes if possible.
What should happen if an insert occurs but your process has died for some reason?
Oracle can now communicate to Java via listener. So if you register for some event, your Java listener will receive that event from the database.
Related
We were looking into rerunning handpicked events from axon. A use case would be along the lines of the following.
If the user's registration event failed even though the command was successful, we want to rerun that particular event for that specific event handler(s).
We looked into using the tracking event processor, but that seems to be of replaying a set of events FROM a specific point in time. However, in our case, if there were 100 events yesterday, we would only want to rerun a particular event in the middle.
At the moment we are in migration of migrating to Axon and as such have decided to go with SubscriptionEventProcessors predominantly as it is more synchronous (i.e. errors are propagated up the command handler). And we do understand that subscription processors are stateless, as in they only process what they received via the event bus.
So I am assuming that we cannot use the tracking processor? and instead, we need to load the particular event and re-push it to the event bus?
How could we achieve this? (With or without the above suggestion)
Also with regards to identifying exceptions, we are thinking of using aspects and logging and reading the particular log line for exceptions. However we did notice a tracing module for axon and spring boot. https://github.com/AxonFramework/extension-tracing However it is mentioned to be in beta and there was not much reference documents we could find yet either. Is there a better more axon based solution to this as well?
To be quick about it, I would use a similar response as I have posted on this question.
The rehash my response over there quickly, it can be summarized by:
Create a Query Model containing the required #EventHandler annotated methods to "re-handle", which you'd provide as input to Axon's AnnotationEventHandlerAdapter constructor. You would call the subsequent AnnotationEventHandlerAdapter with a filtered event stream based on the requirements you have. As a result, the Query Model will be updated to the format you need.
Thus instead of performing it as a form of query because the users requires that information, you would perform the operation upon an exceptional case. Regardless, you will still form the Query Model anew, but just based on a specific set of events.
By the way, when it comes to the choice of Event Processor, I'd still go for the TrackingEventProcessor. Yes it means an event failure would not issue a rollback, but in essence the event states it has occurred. Thus rolling back because something else fails handling that event is incorrect; the command still succeeded, so it should stay as such too.
Lastly, you are looking for logging logic. The Tracing Extension you've shared has just recently been pulled from it's beta status and thus can be used safely. Next, basic logging can already be achieved by configuring the LoggingInterceptor as a MessageHandlerInterceptor and MessageDispatchInterceptor (documentation on this can be found here). And if you are looking to introduce Aspect logic: Axon has a similar mechanism to tie into each message handler out there. Have a look at the HandlerEnhancer/HandlerEnhancerDefintion on this page.
Hope all this helps you out #MilindaD!
I was writing a set of functions for accessing/writing data to the SQLite database in my android application. Since I need to use getWritableDatabase() to get the database instance and this needs to be called in a non-UI thread, I was wondering if there a clean way to specify the same warning in the java docs of these functions?
Also, I needed one more clarification about getting handle over the database instance using getWritableDatabase(). I should call this wherever I need to write things into database right? Or can I call this once at the application level and use the same handle to access db at different places in the app?
There are no fixed rules for such things. You can only rely on conventions/style.
In other words: try to come up with explicit, unambiguous wording and make sure that this part is easy to read and quick to spot in your javadoc (check the generated HTML as well for these properties).
And then be prepared for bug reports from people ignoring your javadoc.
Rather than just leaving a warning in the javadoc, you might add validation, i.e. detect if you're on the UI thread (see How to check if current thread is not main thread), then throw an exception.
Document that exception.
This question already has answers here:
Real time updates from database using JSF/Java EE
(3 answers)
Closed 7 years ago.
The normal way is calling database from java. But as per my scenario some third party application is doing inserts and my application is just reading. In that case is it possible to do something so that whenever there is new data or db data gets updated I want my java to get that data. i.e. any change in db would trigger my java class. By not running any process or threads i.e. like run a process every 2-5 mins to check db that would increase unnecessary load on server and also it wont be live that means anything comes to the db in mean time is missed.
There might be other good solutions to this problem (see my comment), but one possible quick & dirty trick is to write an insert/update trigger that writes the new inserted/updated data to a file.
Now, in order to make things synchronous, you can make that 'file' a named pipe (a FIFO), providing that you run on a *nix system. In this way, you can have the Java code doing a blocking read on the pipe and, every time there is a new insert/update, the MySQL will write to the pipe (be careful to avoid EOFs) and the consumer (your Java code) will unblock itself, process the new data, and then block again, avoiding unnecessary load.
I think there would be no way to do that.
(And belows may not be an answer but a recommend.)
It seems that java and other application connect DB simultaneiusly. I think the problem comes from here.
3rd party app should notify to my app ..
and my app should insert to DB and do what I want. Then there will be no problem like this and more flexible work we can do. And more safe..
There may be some other reason... but I hope you to consider this also..
This problem is for Xamarin Android C# , but if someone can help in java I'm sure I can convert the code over ..
I'm trying to get some sort of automatic notification on a db that data has been inserted / deleted / etc.
There is outside apps that have access to the db in question, that insert / etc..
I've tried a file observer but it misses most of the inserts.
I've tried using content observer but it never fires a onchange
I've tried using the content observer inside a cursor but no onchange happens either.
(if I understand correctly they will only fire if I register a change occurred which is what I don't want)
Now I've discovered that loaders might be a solution..
They seem to have their own observer that fires when the data changes.
If this is also not an answer then perhaps a database trigger of some kind to notify my app the data was modified ?
I really need guidance here.. no idea how to properly implement a loader..
or if the content observer can be sufficient somehow with some sort of auto trigger as such..
OK, So!
The reason that file Observer was missing db inserts is because a SQLite DB is actually a 2 - 3 file object .. if you watch "Example.DB" then you can miss insert that can happen on Example.DB-shm or Example.DB-wal..
The fix to this, and its not a great fix, is to instead watch the folder ..
doing this will catch all inserts/deletes ..
The problem with this is that it will cause multiple OnChange() 's to execute..
So when coding a file observer like this, you have to either call the stopwatch() while you process the call and switch it on afterwards
OR
Have a "Global Variable" (C# guys are gonna swear at me for calling it that)
or static var
that lets the app know that you are already busy on the event so don't execute the code till the previous call has been completed ..
I am creating an app that checks for user locations every half an hour and updates the location of the user in the local database and then runs CRUD queries based on the user's location even when the app is not running. How do i do it ?
I have referred to this http://techtej.blogspot.com.es/2011/03/android-thread-constructspart-4.html article and i am still confused about which is the correct approach for my result ?
There are 4 options according to the article for what i intend to achieve according to me
1) Service : But since i feel it would be a long operation with the local database, i feel i should ignore this one.
2) IntentService : This cannot perform multiple tasks, so i feel this one also should be avoided for me as i have to get the location of the user and scan the database , update the database (3 tasks)
3)Thread : I am not sure how to call this when the app is not open
4) AsyncTask : I am not sure how to call this when the app is not open.
Basically i looking for something like a CRON JOB that runs on a local database while working on the location data.
It would be great if you could link me up to some tutorials and answer with a simple example to make me understand the difference of all 4 methods.
// editted on 16 March :
I have read something about a JobScheduler which is introduced in the API 21, but not sure if it also supports till Gingerbread and is it the right approach for my question
Thanx
When recording the users position use a service with a notification. Just for the sake of creating a morally responsible app that informs the user the app is tracking them. The service by definition runs in the background.
A fused location provider with setinterval(long) 30 minutes gets the interval. Set fastestInterval() to a minute to receive GPS data when other apps are using the GPS.
Have you considered using a SyncAdapter. Its best to schedule jobs at fixed interval and also optimized for battery usage. Also, once started, it can run independently of the app. As per your requirements, I believe this is best suited for your need. You can read about this here. This also removes the corner case of starting the service (generally used) when your device is restarted. Your app will still continue running the scheduled job even if the device gets restarted.
In the SyncAdapter you have to use a ContentProvider so wrap your DB inside a ContentProvider. Also, preferably use a CursorLoader to run longrunning tasks on DB. You should read about CursorLoader. This is a great way to access your resources. Also, you can define an Observer Design Pattern which Observes for changes in a DB and will perform a task when changes are made in DB. This can also be used inside your application itself and also inside SyncAdapter. Cursor Loader is best preferred for background work on DB. You can perform all CRUD Operations using a CursorLoader and ContentProvider.
This cannot perform multiple tasks
Yes, it can. It has only one thread, and so it can only do one simultaneous task.
i have to get the location of the user and scan the database , update the database (3 tasks)
I have no idea why you think that is three tasks. You cannot do them simultaneously.
Your bigger problem with IntentService is that getting location data is asynchronous, and IntentService is not well-suited for calling APIs that themselves are asynchronous.
But since i feel it would be a long operation with the local database, i feel i should ignore this one.
The point behind any service is for "a long operation".
Basically i looking for something like a CRON JOB that runs on a local database while working on the location data
Use AlarmManager to trigger a WakefulBroadcastReceiver, which then triggers a Service. The Service, in onStartCommand(), forks a background thread to (asynchronously) retrieve the location and update the database. The Service can then call completeWakefulIntent() on WakefulBroadcastReceiver, plus stopSelf() with the startId received in onStartCommand() for this work, plus allow the thread to terminate. If no other commands were received in the interim, the service will shut down.
I think you are looking for something similar to WakefulIntentService. This handles all your cases completely.
You can do your location and db related work inside doWakefulWork() of said implementation.
I've done what you are looking for, both with GPS and non-GPS.
The project I took as staring point for the non-GPS solution already does all you need, and is battery-friendly (credits should go to Kenton Price):
https://code.google.com/p/little-fluffy-location-library/
Take a look at it, it works like a charm. Just run it in any device. If you need any help customizing just let me know.
Just edit the "onReceive" method in the "TestBroadcastReceiver" to update your DB.
If you need the GPS solution let me know too, but I dropped it for being a battery killer!
Hope it helps.
1. I think for this requirement, Thread and inside it AsyncTask -- this structure will be useful.
In link provided by you, it is mentioned very nicely here
2. For location related blog, you can check useful materials here :
(1) Difference between Google Map Distance and Directions API
(2) Check this answer also
Hope this will help you