powered by Jive Software

Plug-in & Threads

If I integrate my “Component” using OpenFire mechanism, how does a component get invoked? One thread per invocation?


I have the same question.

I’'am developing a plugin that uses an own low priority thread for some background operations. This thread uses access to the database and sends some packets to connected clients and maybe other components. So it would great to know exactly where I have to synchronize and where not.

Everything I found is the following small hint:

( method IQRouter.route() )


Be careful to enforce concurrency DbC of concurrent by synchronizing any accesses to class resources.

But with which object I have to synchronize?

synchronize (XMPPServer.getInstance().getIQRouter()) {
   /* ...code... */

This doesn’'t make sense, because openfire seems not to synchronize with this class…?


still waiting for an answer

If have spend some hours doing research on this…

Sending packets seems to be synchronized in method SocketConnection.requestWriting() by using an AtomicBoolean. So it should be save to send packets from concurrent threads using componentManager.sendPacket() or one of the specific packet routers directly.

Database connections seem to be synchronized, too. I say this, because openfire uses the Java API (java.sql package) where normally is at least a hint in documentation, if something is not thread save.

In my opinion only access from openfire to our plugin has to be thread safe. E.g. calls to Plugin.processPacket() could happen concurrently. The same goes for other implemented methods from interfaces like UserEventListener, PacketInterceptor, …

Can anyone to affirm the truth of this?



For doing some background work it is maybe better to use Openfire’'s TaskEngine class, and not write your own class, like I did.

I would always assume a hostile (ie, invocation occurs from multiple threads) environment when doing these sorts of things. Especially with architectural changes like clustering on the near horizon. And that means any outside accessor methods (ie, everything inherited from Component and/or Plugin, plus your own outside stuff if you have it) that modifies or accesses your plugin’'s instance data should synchronize on some object.

The only reason to be more specific is optimization. And remember the words of Michael A. Jackson:

  • The First Rule of Program Optimization: Don’'t do it.

  • The Second Rule of Program Optimization (For experts only): Don’'t do it yet.