We are developing a C++ server which will process based on time based events. For example if a particular user has configured a particular task that has to be processed at a particular time, then an event or notification needs to be sent to the C++ server from the database, to initiate the task. The timming configuration is stored in the database, and the C++ server shouldnt poll the database at an interval, but event should be notified from the database at the configured timming.
We are using odatabase and odynaset libraries to connect and access the oracle database 11g.
Kindly request you to provide a solution for the above problem.
One way would be to use dbms_pipe or dbms_alert. ie you connect from your C++ server session to the db (PRO C) and call dbms_pipe/alert which will block until the db, in another session sends data down the pipe that the c++ server will read and then process. for example, if its a simple "GO RUN NOW!" you want to send, then dbms_alert will do.
for example:
where the db controlling session issued this:
another approach is a scheduler approach (
dbms_queue
), which is a message queue approach, in which you'd be polling (dequeueing) against a queue table and would do something when a message arrives.One approach is to use Oracle's Advanced Queuing. For that purpose you need to set up a queue (and a queue table) and write a PL/SQL procedure that waits for the next message in the queue.
The C++ side then calls the PL/SQL procedure, which returns when the next event has occurred.
On the Oracle side, you will need to use DBMS_SCHEDULER or a similar facility to create the event, i.e. to insert a new message into the queue at the appropriate time.
It's still a polling approach. However, there's absolutely no activity between two events.
Update:
Here's some sample code.
Initial setup of the queue (the message contains a numeric and a text value):
Package header:
Package body:
The database server can send a message using the following code:
The C++ server can wait for message (and receive them) calling the stored
sample_queue_pkg.get_next_msg
. The parameteri_max_wait
specifies the maximum time to wait for the next message in seconds. You probably want to implement a loop that waits for the next message and processes it until it receives a signal that the server is about to quit.