Server Callback to Client

Callback parameter to invoke client logic from the server side

Feature Description

The callback parameter method is similar to calling a local callback or listener; you only need to declare which parameter is a callback type in the Spring configuration file. Dubbo will generate a reverse proxy based on a long connection, allowing the server to invoke client logic. You can refer to the example code in the dubbo project.

Use Case

The callback function informs the client of execution results or sends notifications, functioning similarly to asynchronous calls when method execution times are long, such as in approval workflows where the client is notified of approval results.

Usage

Service Interface Example

CallbackService.java

  1. package com.callback;
  2. public interface CallbackService {
  3. void addListener(String key, CallbackListener listener);
  4. }

CallbackListener.java

  1. package com.callback;
  2. public interface CallbackListener {
  3. void changed(String msg);
  4. }

Service Provider Interface Implementation Example

  1. package com.callback.impl;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.Map;
  5. import java.util.concurrent.ConcurrentHashMap;
  6. import com.callback.CallbackListener;
  7. import com.callback.CallbackService;
  8. public class CallbackServiceImpl implements CallbackService {
  9. private final Map<String, CallbackListener> listeners = new ConcurrentHashMap<String, CallbackListener>();
  10. public CallbackServiceImpl() {
  11. Thread t = new Thread(new Runnable() {
  12. public void run() {
  13. while(true) {
  14. try {
  15. for(Map.Entry<String, CallbackListener> entry : listeners.entrySet()){
  16. try {
  17. entry.getValue().changed(getChanged(entry.getKey()));
  18. } catch (Throwable t) {
  19. listeners.remove(entry.getKey());
  20. }
  21. }
  22. Thread.sleep(5000); // Regularly trigger change notification
  23. } catch (Throwable t) { // Fault tolerance
  24. t.printStackTrace();
  25. }
  26. }
  27. }
  28. });
  29. t.setDaemon(true);
  30. t.start();
  31. }
  32. public void addListener(String key, CallbackListener listener) {
  33. listeners.put(key, listener);
  34. listener.changed(getChanged(key)); // Send change notification
  35. }
  36. private String getChanged(String key) {
  37. return "Changed: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
  38. }
  39. }

Service Provider Configuration Example

  1. <bean id="callbackService" class="com.callback.impl.CallbackServiceImpl" />
  2. <dubbo:service interface="com.callback.CallbackService" ref="callbackService" connections="1" callbacks="1000">
  3. <dubbo:method name="addListener">
  4. <dubbo:argument index="1" callback="true" />
  5. <!-- You can also specify by type -->
  6. <!--<dubbo:argument type="com.demo.CallbackListener" callback="true" />-->
  7. </dubbo:method>
  8. </dubbo:service>

Service Consumer Configuration Example

  1. <dubbo:reference id="callbackService" interface="com.callback.CallbackService" />

Service Consumer Calling Example

  1. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:consumer.xml");
  2. context.start();
  3. CallbackService callbackService = (CallbackService) context.getBean("callbackService");
  4. callbackService.addListener("foo.bar", new CallbackListener(){
  5. public void changed(String msg) {
  6. System.out.println("callback1:" + msg);
  7. }
  8. });

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)