支持分布式的callback
程序员文章站
2022-06-06 10:56:34
...
项目背景:
之前在某次培训的分享中,谈到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); } }