Java 如何在ignite messaging lambda函数的remotelisten中调用非静态函数?
我有一个类(TopicListenerImp)监听topic,我想得到来自remoteListen函数的消息,并将其作为参数提供给其他服务类中的另一个非静态函数Java 如何在ignite messaging lambda函数的remotelisten中调用非静态函数?,java,spring-boot,messaging,ignite,Java,Spring Boot,Messaging,Ignite,我有一个类(TopicListenerImp)监听topic,我想得到来自remoteListen函数的消息,并将其作为参数提供给其他服务类中的另一个非静态函数 @Component public class TopicListenerImp implements TopicListener { private NotificationService notificationService; private SubscriptionRepositor
@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));
subscriptionDataOptional.ifPresent(
subscriptionData -> {
if (subscriptionRules.checkRules(subscriptionData) && nfProfile.isPresent())
notificationService.sendEventNotification(
subscriptionData, nfProfile.get(), NF_PROFILE_CHANGED);
});
}
private List<String> parseNfInstanceIdSubscriptionId(String msg) {
List<String> values = asList(msg.split(", "));
return asList(getIdFromMessage(values.get(0)), getIdFromMessage(values.get(1)));
}
private String getIdFromMessage(String msg) {
return msg.substring(msg.indexOf('[') + 1, msg.length() - 1);
}
}
当我静态地注入字段时,它就工作了。但当我这样做时,IDE会发出警告“不要从构造函数方法更新静态变量”。使用一个谓词,该谓词将被序列化并通过网络发送到远程节点。当您在其实现中调用非静态方法时,它会使整个this
对象序列化。它可能会导致意外行为和通过网络发送的大量数据
一般来说,不建议通过网络发送lambda函数,因为它们的序列化依赖于VM,并且它不会让您控制实际发送的内容。如果希望通过网络发送谓词,则创建一个实现谓词接口的类,并使用该类的实例
如果您不需要集群中的所有节点来订阅该主题,那么就足够了。感谢您的评论localListen的工作方式很有魅力,但我有一个问题。localListen也接受谓词,为什么它不需要静态性?远程侦听器的问题来自序列化它的需求。但是本地文件不会发送到任何地方,因此不需要序列化任何内容。
Caused by: org.apache.ignite.binary.BinaryObjectException: Failed to serialize object etc.