1

I'd like to use Publish/Subscribe in our XPages application, in Java, e.g. with Jedis. The application runs in a multi-user setting, and when one user makes some changes to a document we'd like to see those changes reflected on other users' screens. In theory that could be done using PubSub: when the change is applied, a "document modified" message is published and sent to the party or parties that subscribed to this message. The subscriber part I'd like to put in a Thread, so that the object that subscribed can react immediately when the message is received.

The scope for most objects that use subscribe is viewscope, they should be destroyed when viewscope is destroyed. But what happens when the object is abandoned and the subscriber thread is still there? For instance, how can I tell the JVM that the Thread can safely be stopped and scrapped by the garbage collector?

I have yet to try this, so I have no code that I can show, but here's the questions I have:

  • am I right, will the Thread continue to run even when viewscope is destroyed?
  • is there a way to create a garbage-collectable Thread?
  • or maybe: is there some API that does PubSub in the multi-threaded XPages environment?
Selaron
  • 5,617
  • 4
  • 26
  • 38
D.Bugger
  • 2,143
  • 12
  • 18
  • Regarding threads this might be worth reading before you start: [How to run a background task in a servlet based web application?](https://stackoverflow.com/questions/4691132/how-to-run-a-background-task-in-a-servlet-based-web-application) – Selaron Oct 16 '19 at 09:22
  • In JSF 2.3 updates from server can be pushed to the client using websockets, but I don't know how this fits into XPages which I'm not fimiliar with. See [How can server push asynchronous changes to a HTML page created by JSF?](https://stackoverflow.com/questions/3787514/how-can-server-push-asynchronous-changes-to-a-html-page-created-by-jsf) – Selaron Oct 16 '19 at 09:29
  • Not servlet, and not server-client, but server-server. XPages is based on JSF albeit not a recent version. I'd like to inform other parts of a running system that they should be aware that a specific document has been changed, so they can adapt their behaviour accordingly. I already used Jedis for Websockets, but I'd like to use a similar thing internally. – D.Bugger Oct 16 '19 at 09:49

1 Answers1

2

I did something like this years ago. You can find a Pub/Sub example with Guava here: http://hasselba.ch/blog/?p=2158

I am not sure what you are planning with the subscriber thread respectively what the benefit is for your idea.

But to answer your questions: Yes, a thread continues to run until you stop it. You should use an ExecutorService because it helps you with management a lot. If you want to have it "automatically" removed, you just have to do a "shutdown", which then processes all jobs left and stops the ThreadPool.

If you need a server-wide Pub/Sub system, think about an OSGi Plugin which starts / stops automatically with the Domino HTTP task. All objects of this plugin can be used by any application.

Sven Hasselbach
  • 10,425
  • 1
  • 16
  • 26
  • I'm using Jedis for PubSub already, mainly for server-client (browser) communication. Would it be possible to subscribe to a message without starting a Thread? Can that be done without losing the NotesContext? – D.Bugger Oct 16 '19 at 10:08
  • @D.Bugger: You will loose the NotesContext when creating your own threads. But why do you need it for Pub/Sub? – Sven Hasselbach Oct 16 '19 at 11:20
  • Now trying without creating a Thread, but it sort of hangs the HTTP task. Jedis isn't thread-safe, so that may be my problem here. I want to use PubSub because one user can change a document, which is part of a workflow application, and another user might need that information on his screen. I want to send a system-wide message that a document was changed, so that other parts can react, even when they are in some other user's viewscope or sessionscope. – D.Bugger Oct 16 '19 at 12:00
  • @D.Bugger: Have a look at JedisPool, it is thread safe. For the "message" that user X has changed document Y, you don't need the NotesContext. This is a simple POJO. – Sven Hasselbach Oct 16 '19 at 12:44
  • java.lang.NoClassDefFoundError: org.apache.commons.pool2.PooledObjectFactory – D.Bugger Oct 16 '19 at 16:56