package edu.javacourse.jms;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.Calendar;

@MessageDriven(name = "TestMDB3", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/test")
})
public class MessageReceiverBean3 implements MessageListener {
    
    private static final Logger log = LoggerFactory.getLogger(MessageReceiverBean3.class);

    @PostConstruct
    public void before() {
        log.debug("Bean 3 PostConstruct");
    }

    @PreDestroy
    public void after() {
        log.debug("Bean 3 PreDestroy");
    }

    @Override
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                Thread.sleep(2000);
                TextMessage textMessage = (TextMessage) message;
                log.debug("Bean 3 from topic: {}", textMessage.getText());
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }
    }

}

@@ -155,16 +155,50 @@
 estroy;%0A
+import javax.annotation.Resource;%0A
 import j
@@ -264,16 +264,55 @@
 Driven;%0A
+import javax.ejb.MessageDrivenContext;%0A
 import j
@@ -1041,24 +1041,80 @@
 y%22);%0A    %7D%0A%0A
+    @Resource%0A    public MessageDrivenContext context;%0A%0A
     @Overrid
@@ -1257,9 +1257,9 @@
 eep(
-2
+1
 000)
@@ -1325,16 +1325,394 @@
 essage;%0A
+                final String text = textMessage.getText();%0A                if (text.contains(%22This is message 3%22) %7C%7C text.contains(%22This is message 5%22) %7C%7C text.contains(%22This is message 7%22)) %7B%0A                    log.debug(%22*** Bean 3 Throw message with text'%22  + text + %22' away...%22);%0A                    context.setRollbackOnly();%0A                    return;%0A                %7D%0A
         
@@ -1758,33 +1758,16 @@
 %7D%22, text
-Message.getText()
 );%0A     

Update Topic MDB for transaction rollback