
/*
 *  Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
*/
package oracle.javacloud.mdb;

import java.util.logging.*;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.*;

@MessageDriven(mappedName = "local-topic", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "connectionFactoryType", propertyValue = "javax.jms.ConnectionFactory"),
    @ActivationConfigProperty(propertyName = "connectionFactoryJndiName", propertyValue="local-connection-factory"),
    @ActivationConfigProperty(propertyName = "topicMessagesDistributionMode", propertyValue="One-Copy-Per-Application"),
})
public class TopicMessageBean implements MessageListener {

    public TopicMessageBean() {
    }
    
    @Override
    public void onMessage(Message message) {

        Logger logger = Logger.getLogger(TopicMessageBean.class.getName());
        try {
            if (message instanceof TextMessage) {
                System.out.println("JMS message received at TopicMessageBean : "+((TextMessage) message).getText());
                logger.log(Level.INFO, "JMS message received at TopicMessageBean : "+ ((javax.jms.TextMessage) message).getText());

            } else {
                System.out.println(message.getJMSMessageID());
            }
        } catch (JMSException ex) {
            Logger.getLogger(TopicMessageBean.class.getName()).log(Level.SEVERE, "Error: ", ex);
        }    
    
    }
}

