0

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!

Luiz E.
  • 6,769
  • 10
  • 58
  • 98

2 Answers2

2

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.

Rohit
  • 2,132
  • 1
  • 15
  • 24
  • I guess I'm not being clear enough. Each time I get a message, I explicity call the producer: `eventNotifier.saveMessage`. then, I can see in the logs the messages I put around the `save` in the `MessageService`, but no JPA logs. – Luiz E. Sep 09 '16 at 14:35
  • Could you share complete consumer code? Or atleast its complete structure with empty methods? – Rohit Sep 09 '16 at 14:42
  • yes: https://github.com/inkdrop/chathub-backend/blob/develop/src/main/java/com/inkdrop/app/consumers/EventConsumer.java – Luiz E. Sep 09 '16 at 14:44
  • With out the reactor stuff the `MessageRepository .save()` method saves your entity? This looks to me related to spring data configuration thing. – Rohit Sep 09 '16 at 14:55
1

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.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • hmm, but I don't get it? I have other methods in the `EventConsumer`, like push the message to firebase, log it, etc. and they are being called whenever I want with no problem – Luiz E. Sep 09 '16 at 14:21