slackapi / java-slack-sdk

Slack Developer Kit (including Bolt for Java) for any JVM language
https://slack.dev/java-slack-sdk/
MIT License
568 stars 210 forks source link

How to un-register/re-register a message listener #1305

Closed lz000 closed 2 months ago

lz000 commented 3 months ago

I know we can use app.message to listen for a specific pattern. But is there a way to dynamically un-register an event listener for a pattern and re-register the event listener again? and replace event listener?

zimeg commented 3 months ago

Hey @lz000 👋 Thanks for the interesting question! When setting up an event listener, like app.message, the event is registered to an events dispatcher for your app and I don't believe these events can be deregistered once added.

Replacing an existing event listener with a different one, however, does seem possible by replacing an existing handler with the latest. This applies to most events except for message, which has handlers appended to the list of listeners instead.

I hope this helps but let me know if this is giving you any troubles! I'm also curious if you're trying to re-register certain events or the message one specifically? Some logic within a listener might be another approach to deregister a listener without removing it from the list of event handlers too, but might not be ideal.

seratch commented 2 months ago

Specifically talking about app.message listener user case, I would recommend having one large app.event(MessageEvent.class) listener and having your own dispatcher within it. This approach offers greater flexibility and eliminates the need to focus on Bolt-specific details.

lz000 commented 2 months ago

@seratch thanks. That wouldn't work for our case, because when we want un-register, we don't want the app to get message from slack at all. With your approach, the app still retrieve message but just ignored. We have 2 instances. We want 1 instance retrieve message, they other one unregister so it does not compete message with the first one. We want to make this dynamic, so we can control which instance to register and unregister

seratch commented 2 months ago

Ah I see. Unfortunately, bolt and the Slack platform do not provide a feature that meets your needs. The only approach I can suggest is to have a proxy server in front of your bolt app instances and let the proxy server do a kind of load balancing like you mentioned.

I understand this is not a great answer for you but we don’t have anything else to share on this matter. If everything is clear to you so far, would you mind closing this issue? You can submit new ones whenever you have new but similar questions.