dubbo高级应用回调

dubbo高级应用回调

 

 

dubbo的回调和本地的回调机制一致,在服务提供端配置回调的方法时为了dubbo能把消费端的回调对像传到提供端

参数回调实现了在客户端调用服务时,将回调方法做为参数传递给服务端。当服务端提供服务时会调用执行客户回调方法,从而执行客户端自定义的一些操作

 

 

 

参数回调方式与调用本地callback或listener相同,只需要在Spring的配置文件中声明哪个参数是callback类型即可,Dubbo将基于长连接生成反向代理,这样就可以从服务器端调用客户端逻辑。
2.0.6及其以上版本支持

代码参见:https://github.com/alibaba/dubbo/tree/master/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/callback

(1) 共享服务接口:

服务接口示例:

CallbackService.java
package com.callback;
 
public interface CallbackService {
     void addListener(String key, CallbackListener listener);
}
CallbackListener.java
package com.callback;
 
public interface CallbackListener {
     void changed(String msg);
}

(2) 服务提供者:

服务提供者接口实现示例:

CallbackServiceImpl.java
package com.callback.impl;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
import com.callback.CallbackListener;
import com.callback.CallbackService;
 
public class CallbackServiceImpl implements CallbackService {
     
     private final Map listeners = new ConcurrentHashMap();
  
     public CallbackServiceImpl() {
         Thread t = new Thread( new Runnable() {
             public void run() {
                 while ( true ) {
                     try {
                         for (Map.Entry entry : listeners.entrySet()){
                            try {
                                entry.getValue().changed(getChanged(entry.getKey()));
                            } catch (Throwable t) {
                                listeners.remove(entry.getKey());
                            }
                         }
                         Thread.sleep( 5000 ); // 定时触发变更通知
                     } catch (Throwable t) { // 防御容错
                         t.printStackTrace();
                     }
                 }
             }
         });
         t.setDaemon( true );
         t.start();
     }
  
     public void addListener(String key, CallbackListener listener) {
         listeners.put(key, listener);
         listener.changed(getChanged(key)); // 发送变更通知(不用定时方式)
     }
     
     private String getChanged(String key) {
         return "Changed: " + new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format( new Date());
     }
 
}

服务提供者配置示例:

< bean id = "callbackService" class = "com.callback.impl.CallbackServiceImpl" />
< dubbo:service interface = "com.callback.CallbackService" ref = "callbackService" connections = "1" callbacks = "1000" >
     < dubbo:method name = "addListener" >
         < dubbo:argument index = "1" callback = "true" />
        
        
     dubbo:method >
dubbo:service >

(2) 服务消费者:

服务消费者配置示例:

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

服务消费者调用示例:

CallbackServiceTest.java
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "classpath:consumer.xml" );
context.start();
 
CallbackService callbackService = (CallbackService) context.getBean( "callbackService" );
 
callbackService.addListener( "http://10.20.160.198/wiki/display/dubbo/foo.bar" , new CallbackListener(){
     public void changed(String msg) {
         System.out.println( "callback1:" + msg);
     }
});
 
 

 执行结果

Java代码   收藏代码
  1. callback1:Changed: 2016-06-17 14:15:05  
  2. callback2:Changed: 2016-06-17 14:15:05  
  3. callback2:Changed: 2016-06-17 14:15:06  
  4. callback1:Changed: 2016-06-17 14:15:06  
  5. callback2:Changed: 2016-06-17 14:15:11

你可能感兴趣的:(dubbo)