Reputation: 7279
I have the following code:
Controller:
eventNotifier.saveMessage(buildMessage(message, room, sender));
EventNotifier:
public void saveMessage(Message message){
r.notify(EventConsumer.NEW_MESSAGE, Event.wrap(message));
}
EventConsumer:
@PostConstruct
public void onStartUp() {
r.on(Selectors.R(MESSAGE_SAVED), createEventAndPush());
r.on(Selectors.R(NEW_MESSAGE), saveMessage());
r.on(Selectors.R(EVENT), pushToMixpanel());
}
public Consumer<Event<Message>> saveMessage(){
System.err.println("Calling save async");
return event -> messageRepo.save(event.getData());
}
turns out that the method saveMessage
is never being called. I saw it being called once, during the startup server process.
I'm not sure this is reactor-related or spring related.
edit: I moved the saving process to a service, nothing changed
edit2: I logged the service action to see what is going on:
@Service
@Slf4j
public class MessageService {
@Autowired MessageRepository messageRepo;
@Transactional
public void save(Message m){
try{
log.info("Saving...");
messageRepo.save(m);
log.info("Saved");
}catch(Exception e){
e.printStackTrace();
}
}
the output in console is:
2016-09-09 16:24:42.152 INFO 45041 --- [ wa-chub-2] com.inkdrop.app.services.MessageService : Saving...
2016-09-09 16:24:42.154 INFO 45041 --- [ wa-chub-2] com.inkdrop.app.services.MessageService : Saved
no logs from JPA, nothing!
Upvotes: 0
Views: 80
Reputation: 2152
Method annotated with @PostConstruct
will only be called once by the spring context during bean creation. The onStartup()
method explictly calls the saveMessage()
method hence the you see the call once.
To save it each time you have to call it explicitly, its not going to be done magically.
Upvotes: 2
Reputation: 24581
I will quote @PostContruct
Javadoc:
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.
So it will be executed only once after your bean is initialized.
Upvotes: 1