我有一个监听主题的类(TopicListenerImp),我想从remoteListen函数获取消息,并将其作为参数提供给其他服务类中的另一个非静态函数。 @Component public class TopicListenerImp implements TopicListener { private NotificationService notificationService; private SubscriptionRepository subscriptionRepository; private SubscriptionRules subscriptionRules; private NFInstancesService nfInstancesService; private Ignite ignite; public TopicListenerImp( SubscriptionRules subscriptionRules, NotificationService notificationService, SubscriptionRepository subscriptionRepository, Ignite ignite, NFInstancesService nfInstancesService) { this.subscriptionRules = subscriptionRules; this.notificationService = notificationService; this.subscriptionRepository = subscriptionRepository; this.nfInstancesService = nfInstancesService; this.ignite = ignite; } @Bean public void startTopicListening() { IgniteMessaging rmtMsg = ignite.message(ignite.cluster().forLocal()); rmtMsg.remoteListen( "SUSPEND", (nodeId, msg) -> { notifyIfSubscriptionExist((String) msg); //here where I used the message that comes from topic return true; }); } public void notifyIfSubscriptionExist(String msg) { List<String> nfInstanceIdSubscriptionId = parseNfInstanceIdSubscriptionId(msg); Optional<NFProfile> nfProfile = nfInstancesService.getNFInstance(nfInstanceIdSubscriptionId.get(0)); Optional<SubscriptionData> subscriptionDataOptional = subscriptionRepository.getSubscriptionData(nfInstanceIdSubscriptionId.get(1)); }); }但我收到以下错误; Caused by: org.apache.ignite.binary.BinaryObjectException: Failed to serialize object etc.当我静态注入字段时,它起作用了。但是当我这样做时,IDE 会发出警告“不要从构造函数方法更新静态变量”。
1 回答
潇潇雨雨
TA贡献1833条经验 获得超4个赞
RemoteListen采用一个谓词,该谓词将被序列化并通过网络发送到远程节点。当您在其实现中调用非静态方法时,它会使整个this
对象被序列化。它可能会导致意外行为和通过网络发送大量数据。
一般来说,不建议通过网络发送 lambda 函数,因为它们的序列化依赖于 VM,并且无法让您控制实际发送的内容。如果您想通过网络发送谓词,则创建一个实现谓词接口的类并使用该类的实例。
如果不需要集群中的所有节点都订阅该主题,那么localListen就足够了。
添加回答
举报
0/150
提交
取消