I implement a callable task that set some data to the engine and execute "session.think()"
created a pool of 7 threads - each thread need to do some calculation for a single day of a week (Sunday,Monday..,Saturday)
I would like to know if I need to create a new session for each task I submit(for each new thread that start)
or should I use single session that is shard by the callable tasks I have ?
is session is thread safe ? regarding the fact the every think() execution changes session state.
what is the good practice when working with OPA + threads ?
Another thing I encounter that if the "session.think()" is not declared in synchronized method I am getting this Error
Exception: IllegalStateException - Think cycle has not stabilised after 20 iterations. This is probably due to a fault in the inferencing listener. and I am not implemented any listener
At first glance it looks to me like you are possibly over-complicating your interactions with the engine. The OPA engine can do a lot for you - and you should let it. In almost all cases, it is better to push all the data required to determine all the outputs into the engine at the start, call think() once, then read all the results out.
You should take advantage of the implicit operations that occur during the think() cycle, such as the set-based operations (minimum, maximum, sum, etc) and the inherent iteration that occurs over entity instances.
In general, the low-level control of rule/operation execution should be controlled by the rules (and the engine) and not programmatically.
In your case my advice would be to look at modeling your days of the week as entities, create 7 of them, and have the rules operate across the collection during a single think(). Assigning one of these higher-level (whole week) determinations to an individual thread would probably be a better level to do your thread management.
I agree with the previous analysis but to answer your specific question, the engine is thread safe, in fact it only exists a singleton, the rulebase is also thread safe which is excellent as it can be quite big and take a while to load. The session is not thread safe and I suspect the error you are seeing is due to multiple threads accessing the same session. It is not strictly necessary to discard the session after each cycle but creating a new one is relatively quick and this may well be the best way to ensure you don't have any data hanging around that you no longer want.