支持分布式的callback

项目背景:

       之前在某次培训的分享中,谈到rxjava、vertx等开源框架,也了解到callback概念,在分享的中某位童鞋谈到分布式下callback如何处理,说实话在当时的环境能够通过几句话描述callback都比较困难,更何况描述分布式下的callback,所以本文我想通过基于jgroup的方式,更好阐述我的理解如何处理分布式下的callback。

 

 

 



 

 

public class ReplyingCallback implements CommandCallback<Object, Object> {

    private final JChannel channel;
    private final Serializer serializer;

    private static final Logger logger = LoggerFactory.getLogger(ReplyingCallback.class);
    private final Address address;

    public ReplyingCallback(JChannel channel, Address address, Serializer serializer) {
        this.address = address;
        this.channel = channel;
        this.serializer = serializer;
    }

    @Override
    public void onSend(CommandMessage<?> commandMessage, Object result) {
        try {
            channel.send(address, new ReplyMessage(commandMessage.getIdentifier(),
                                                   result,
                                                   null, serializer));
        } catch (Exception e) {
            logger.error("Unable to send reply to command [name: {}, id: {}]. ",
                         commandMessage.getCommandName(), commandMessage.getIdentifier(), e);
            throw new CommandResponseProcessingFailedException(String.format(
                    "An error occurred while attempting to process command response of type : %s, Exception Message: %s",
                    result.getClass().getName(), e.getMessage()), e);
        }
    }

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值