我有一个生产者在Kafka中发送一些对象(Spring Boot Application) 我有一个配置了neo4j的消费者(我正在使用Spring-Data和Spring Boot)
监听器的代码:
@Component
@Slf4j
public class KafkaReceiver {
@Autowired
AssetService assetService;
@Transactional
@KafkaListener(topics = "portfolio")
public void receive(ConsumerRecord<?,?> consumerRecord) throws Exception{
log.debug("################## START RECEIVE ");
String value = consumerRecord.value().toString();
ObjectMapper objectMapper = new ObjectMapper();
log.debug("@@@@@@@ OBJECTS BEFORE : " + assetService.count());
Asset asset = objectMapper.readValue(value, Asset.class);
Asset s = assetService.addAsset(asset);
log.debug("@@@@@@@ OBJECTS AFTER : " + assetService.count());
log.debug("################## END RECEIVE ");
}
}
和AssetService的代码:
@Service
public class AssetService {
@Autowired
AssetRepository assetRepository;
@Autowired
Session neo4jSession;
@Autowired
private SessionFactory sessionFactory;
@Transactional(isolation = Isolation.READ_COMMITTED)
public Asset addAsset(Asset asset){
Asset s = assetRepository.save(asset);
return s;
}
@Transactional(readOnly = true)
public long count(){
return assetRepository.count();
}
}
(完整代码也可在github上找到:https://github.com/jonleb/assetmanager)
当我运行应用程序时,第一次监听器使用该服务时,数据将保留在neo4j中,但之后并非如此。我必须重新启动应用程序以再次存储对象。 我不明白为什么。我尝试改变交易,但经过一天的研究,我没有任何新的想法。
你能帮帮我吗?
由于