整体项目结构
MyService 为服务接口
CallbackListener 为回调接口
MyServiceImpl 为服务实现
CallbackListenerImpl 为回调实现
CallbackProvider 为Provider启动类
CallbackConsumer 为Consumer启动类
callback-provider.xml 为Provider配置
callback-consumer.xml 为Consumer配置
代码参考
具体实现
MyService
package org.apache.dubbo.samples.callback.api;
public interface CallbackListener {
void changed(String msg);
}
CallbackListener
package org.apache.dubbo.samples.callback.api;
public interface CallbackListener {
void changed(String msg);
}
MyServiceImpl
public class CallbackListenerImpl implements CallbackListener {
@Override
public void changed(String msg) {
System.out.println("callback: " + msg);
}
}
CallbackListenerImpl
package org.apache.dubbo.samples.callback.impl;
import org.apache.dubbo.samples.callback.api.CallbackListener;
import org.apache.dubbo.samples.callback.api.MyService;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyServiceImpl implements MyService {
@Override
public void gogogo(String key, CallbackListener listener) {
listener.changed(getChanged(1));
listener.changed(getChanged(2));
listener.changed(getChanged(3));
}
private String getChanged(int batchId) {
return String.format("返回批次【%s】: ", batchId)
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
}
CallbackProvider
package org.apache.dubbo.samples.callback;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.concurrent.CountDownLatch;
public class CallbackProvider {
public static void main(String[] args) throws Exception {
new EmbeddedZooKeeper(2181, false).start();
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/callback-provider.xml");
context.start();
System.out.println("dubbo service started");
new CountDownLatch(1).await();
}
}
CallbackConsumer
public class CallbackConsumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/callback-consumer.xml");
context.start();
MyService callbackService = context.getBean("myService", MyService.class);
CallbackListener callbackListener = context.getBean("callback", CallbackListener.class);
callbackService.gogogo("foo.bar", callbackListener);
}
}
callback-provider.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder/>
<dubbo:application name="callback-provider"/>
<dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<bean id="myService" class="org.apache.dubbo.samples.callback.impl.MyServiceImpl"/>
<dubbo:service
interface="org.apache.dubbo.samples.callback.api.MyService"
ref="myService"
connections="1"
callbacks="1000">
<dubbo:method name="gogogo">
<dubbo:argument index="1" callback="true"/>
</dubbo:method>
</dubbo:service>
</beans>
callback-consumer.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:spring="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder/>
<dubbo:application name="callback-consumer"/>
<dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>
<dubbo:reference id="myService" interface="org.apache.dubbo.samples.callback.api.MyService"/>
<bean id="callback" class="org.apache.dubbo.samples.callback.impl.CallbackListenerImpl"/>
</beans>
最终输出
说明
服务(MyService)配置为Callback后,允许Provider在处理业务时,通过回调接口(CallbackListener)去回调Consumer。