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..
Related
I'm currently switching the way data are stored into my app from sqlite to Firestore for the sake of online support. Even if this is a big leap and will mean a lot of changes into my current architecture. I wanted to know if there is a way to sort of emulate an Adapter class (like the one used for SQlite), so I can keep those changes to bear the minimum by at least resuing my methods names and return types and avoid to write explicitly each document read/write where I need them.
What I tried at first in my so called "Adapter like class" was to load the document's data inside a dedicated object in my "adapter class"'s constructor. But it obviously didn't work because Firebase queries are async like explained here:
Is there a way to write this database call into a function easily?
Is what I want to do possible ? Or am I doomed to redo everything ?
Short answer: yes, but you shouldn't
If you absolutely must:
You can wrap the async calls in the Firebase API by using CountDownLatch to create your own Future implementation. Future is a Java Interface similar to Promises in JavaScript, which allow you to pass around the "future result" of an async computation until you absolutely need it.
Check this StackOverflow answer for a sample implementation.
You would then implement your "Adapter-like class" with this wrapper, which would return a Future, and the methods in your class would return future.get(). This call blocks the thread which executes it and when the Future resolves, the call evaluates to the value contained in the Future ("unwrapping" it).
Here's why you shouldn't:
In Android, it's usually a Very Bad Idea to block the UI thread, so if your current Adapter is already a synchronous call, it will keep blocking until the Firebase query finishes.
If you are already blocking the UI thread with your SQLite queries, and think you might be fine because of that, consider the possibility that this is not a problem now just because the SQLite queries are too fast for it to matter. When you switch to Firestore, the queries are likely to take much longer on average and have way more variance in their response time due to different network conditions, etc.
If it's not very unfeasible to consider refactoring to use the async APIs, seriously consider it. Your queries are going to take longer now and you might need "loading" indicators anyway, so using the async APIs will help with that. You may also see opportunities to run queries in parallel when you need both but they don't depend on each other, and keeping it async allows you to do that.
This might not have mattered before if the queries were fast enough that sequential vs. parallel didn't matter much, but now it may.
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.
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.
I am java and php programmer.
In java i can use static class/method so that anyone can use the same one time created class during run-time.
But for php how to do it since it is script based and only run while we refreshing the page?
My main objective is, I want to use syncronized class/method so that it wont clash while executing the PHP...
Need your help to give input.
Thanks
Update:
I am doing portal like multi level marketing(mlm)
Once register a member, we should pay bonus to the uplines
I don't want immidiately calculate the bonus because it is risky and could take some time to finish, so is is better just to register the member and show successfull.
My idea is, after registration, just invoke another class to run bonus with syncronized method so that the bonus calculation will not disturb by another registration.
Given that a php scripts runs from new every sinlge time a "static" class would not be very different from an ordinary class.
If you want to store some sort of state or preserve some data between runs of a php program then there are a number of options.
SESSION variables can be used to store data between requests from a single users as long as he keeps the session open.
COOKIES can be used to store data which persists between sessions as long as the user is using the same browser, on hte same machine and hasnt emptied the cookie jar.
memchached and similar packages can be used to store data and make it available to any php program on the server.
Databases are the most scalable solution as they will persist data between sessions, and between servers. There is some overhead involved is establishing connections and retrieving the data compared with the other solutions.
PHP is shared-nothing. Everything just lives for the Request. If you want to share information between Requests, you have to implement some additional technology layer that can do so. Or look into process control, shared memory segments and semaphores. The latter three are uncommon usage in PHP though. And all of the above will still be asynchronous.
To my knowledge, there is no way to update class Foo in one Request and have it change state immediately in a concurrent Request with PHP.
I have already posted a question today. This question is about the same project but unrelated. I am developing an application for the Lego NXT Mindstorm robot. I have two robots and a GUI running on a PC.
In leJOS NXJ you can only use one input reader. This means that you can't connect the PC to two robots directly and let the two robots connect to each other directly. So this is what i have done. I have connected the PC to the two robots directly and and when the two robots want to communicate directly, i send their messages through the GUI.
There is a whole lot of communication between the GUI and the robots as well as between the robots themselves. For this reason anytime i write data to the output stream it seems that some of the data are overwritten by others and the system is not working correctly as suppose to.
I have been advice to write a class that will hold a collection(Queue) object so that anytime the robot want to send something, it add it to the collection(Queue) and from that class which hold the collection object, there will be a method so that it checks the collection constantly and whenever it is not empty, it sends the data in the collection to the output stream.
It means that whenever the data in the collection are been sent to the output stream, it is possible a new data is been added.
Some people suggested to me of using ArrayBlockQueue and etc.. but those classes are not available in the class.jar file which the robot uses.
The collections classes that i know in this jar file are Vectors and Queue.
I am asking if someone can help me by giving me ideas of how to implement such class. A method in the class will check from time to time if there are data inside the collection and it will send them through the output stream. While it is sending , it is possible that new elements are being added.
Since the data are being sent from one place, no data will overwrite the other. It sounds to me as a good idea.
All your suggestions are welcome.
Thanks.
Vector is good because (at least in JavaSE - I don't know what Mindstorms uses) it's synchronized, so all calls are atomic - if another thread tries to add something to the Vector when you're removing from it, it will block until you have finished, avoiding the issue where data can get lost.
Alternatively, you may want to have a look at the synchronization wrappers in the Collections class.
Alternatively, you could do your own implementation of a blocking queue by subclassing a standard Queue. Although more complicated, a blocking queue is a better solution, as it avoids a busy wait, where you repeatedly check the queue and are each time told it is empty.