美文网首页
Cdubbo回调

Cdubbo回调

作者: 昵称全尼马被注册了 | 来源:发表于2020-01-13 21:22 被阅读0次

整体项目结构

image.png
MyService 为服务接口
CallbackListener 为回调接口
MyServiceImpl 为服务实现
CallbackListenerImpl 为回调实现
CallbackProvider 为Provider启动类
CallbackConsumer 为Consumer启动类
callback-provider.xml 为Provider配置
callback-consumer.xml 为Consumer配置

代码参考

参数回调example

具体实现

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>

最终输出

image.png

说明

服务(MyService)配置为Callback后,允许Provider在处理业务时,通过回调接口(CallbackListener)去回调Consumer。

相关文章

  • Cdubbo回调

    整体项目结构 代码参考 参数回调example 具体实现 MyService CallbackListener M...

  • Promise

    回调 把一个函数A传给另一个函数B调用,那么A就是回调函数。 回调地狱 回调套回调套回调套回调套回调套回调套回调....

  • 回调、同步回调、异步回调

    异步消息的传递-回调机制 原文地址:https://www.ibm.com/developerworks/cn/l...

  • 前端入门11 -- JavaScript之Promise

    回调函数 回调函数分为两种类型,分别为同步回调与异步回调; 同步回调:会立即执行,完全执行完了才结束,不会放入回调...

  • 回调函数与promise

    回调 把一个函数A传给另一个函数B调用,那么A就是回调函数 具名回调写法 匿名回调写法 多层嵌套的匿名回调(回调地...

  • 回调函数与promise

    回调 把一个函数A传给另一个函数B调用,那么A就是回调函数 具名回调写法 匿名回调写法 多层嵌套的匿名回调(回调地...

  • Kotlin使用接口回调

    1.Java中的接口回调实现(支持多方法回调) 声明回调接口,初始化接口 使用接口回调(无参数) 使用接口回调(带...

  • Promise

    回调 把一个函数A传给另一个函数B调用,那么A就是回调函数一个最基本的具名回调匿名回调 回调地狱匿名回调嵌套过多层...

  • [swift]回调block回调

    在OC中习惯应用block进行事件回调,到swift中依然想找到这种简洁的回调事件,下面将介绍如何在swift中使...

  • 回调函数,优化回调

    $.Callbacks(['once']或者['memory']或者['unique']或者['stopOnFal...

网友评论

      本文标题:Cdubbo回调

      本文链接:https://www.haomeiwen.com/subject/zrmnactx.html