Search in sources :

Example 66 with Topic

use of javax.jms.Topic in project ignite by apache.

the class JmsStreamer method start.

/**
 * Starts streamer.
 *
 * @throws IgniteException If failed.
 */
public void start() throws IgniteException {
    if (!stopped)
        throw new IgniteException("Attempted to start an already started JMS Streamer");
    try {
        A.notNull(getStreamer(), "streamer");
        A.notNull(getIgnite(), "ignite");
        log = getIgnite().log();
        A.notNull(transformer, "message transformer");
        A.notNull(connectionFactory, "connection factory");
        A.ensure(threads > 0, "threads > 0");
        // handle batched && transacted parameter interaction
        if (batched && !transacted) {
            log.warning("Starting a Batched JMS Streamer without transacted flag = true. Setting it automatically.");
            transacted = true;
        }
        // handle batch completion criteria
        if (batched) {
            A.ensure(batchClosureMillis > 0 || batchClosureSize > 0, "at least one of batch closure size or " + "batch closure frequency must be specified when using batch consumption");
        }
        // check the parameters needed for durable subscriptions, if enabled
        if (durableSubscription) {
            A.notNullOrEmpty(clientId, "client id is compulsory when using durable subscriptions");
            A.notNullOrEmpty(durableSubscriptionName, "durable subscription name is compulsory when using " + "durable subscriptions");
        }
        // else make sure that the destinationName and the destinationType are valid
        if (destination == null) {
            A.notNull(destinationType, "destination type");
            A.ensure(destinationType.isAssignableFrom(Queue.class) || destinationType.isAssignableFrom(Topic.class), "this streamer can only handle Queues or Topics.");
            A.notNullOrEmpty(destinationName, "destination or destination name");
        } else if (destination instanceof Queue) {
            destinationType = Queue.class;
        } else if (destination instanceof Topic) {
            destinationType = Topic.class;
        } else {
            throw new IllegalArgumentException("Invalid destination object. Can only handle Queues or Topics.");
        }
        // create a new connection and the client iD if relevant.
        connection = connectionFactory.createConnection();
        if (clientId != null && clientId.trim().length() > 0) {
            connection.setClientID(clientId.trim());
        }
        // build the JMS objects
        if (destinationType == Queue.class) {
            initializeJmsObjectsForQueue();
        } else {
            initializeJmsObjectsForTopic();
        }
        stopped = false;
        // start the JMS connection
        connection.start();
        // set up the scheduler service for committing batches
        if (batched && batchClosureMillis > 0) {
            scheduler = Executors.newScheduledThreadPool(1);
            scheduler.schedule(new Runnable() {

                @Override
                public void run() {
                    for (Session session : sessions) {
                        try {
                            session.commit();
                            if (log.isDebugEnabled()) {
                                log.debug("Committing session from time-based batch completion [session=" + session + "]");
                            }
                        } catch (JMSException ignored) {
                            log.warning("Error while committing session: from batch time-based completion " + "[session=" + session + "]");
                        }
                    }
                    for (IgniteJmsMessageListener ml : listeners) {
                        ml.resetBatchCounter();
                    }
                }
            }, batchClosureMillis, TimeUnit.MILLISECONDS);
        }
    } catch (Throwable t) {
        throw new IgniteException("Exception while initializing JmsStreamer", t);
    }
}
Also used : IgniteException(org.apache.ignite.IgniteException) JMSException(javax.jms.JMSException) Topic(javax.jms.Topic) Queue(javax.jms.Queue) Session(javax.jms.Session)

Example 67 with Topic

use of javax.jms.Topic in project tech by ffyyhh995511.

the class TopicReceive method init.

public void init() throws Exception {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
    Connection connection = connectionFactory.createConnection();
    connection.start();
    session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic("fyhTopic");
    consumer = session.createConsumer(topic);
    consumer.setMessageListener(new MessageListener() {

        public void onMessage(Message message) {
            TextMessage tm = (TextMessage) message;
            System.out.println(tm);
            try {
                System.out.println(tm.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) Topic(javax.jms.Topic) TextMessage(javax.jms.TextMessage)

Example 68 with Topic

use of javax.jms.Topic in project tech by ffyyhh995511.

the class TopicRequest method init.

public void init() throws Exception {
    // ConnectionFactory连接工厂,JMS用它创建连接
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
    // 从构造工厂中得到连接对象
    connection = connectionFactory.createConnection();
    // 启动
    connection.start();
    // 获取连接操作
    session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTopic("fyhTopic");
    producer = session.createProducer(topic);
    // 设置不持久化
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Topic(javax.jms.Topic)

Example 69 with Topic

use of javax.jms.Topic in project cxf by apache.

the class JmsPublisher method notify.

@Override
public void notify(NotificationMessageHolderType messageHolder) {
    Session session = null;
    try {
        Topic topic = topicConverter.toActiveMQTopic(messageHolder.getTopic());
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(topic);
        Notify notify = new Notify();
        notify.getNotificationMessage().add(messageHolder);
        StringWriter writer = new StringWriter();
        jaxbContext.createMarshaller().marshal(notify, writer);
        Message message = session.createTextMessage(writer.toString());
        producer.send(message);
    } catch (JMSException e) {
        LOGGER.log(Level.WARNING, "Error dispatching message", e);
    } catch (JAXBException e) {
        LOGGER.log(Level.WARNING, "Error dispatching message", e);
    } catch (InvalidTopicException e) {
        LOGGER.log(Level.WARNING, "Error dispatching message", e);
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                LOGGER.log(Level.FINE, "Error closing session", e);
            }
        }
    }
}
Also used : StringWriter(java.io.StringWriter) Message(javax.jms.Message) Notify(org.oasis_open.docs.wsn.b_2.Notify) JAXBException(javax.xml.bind.JAXBException) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) Session(javax.jms.Session)

Aggregations

Topic (javax.jms.Topic)69 Session (javax.jms.Session)40 MessageProducer (javax.jms.MessageProducer)34 JMSException (javax.jms.JMSException)22 NamingException (javax.naming.NamingException)14 TextMessage (javax.jms.TextMessage)13 Message (javax.jms.Message)9 LaserCreateException (cern.laser.business.LaserCreateException)6 LaserRuntimeException (cern.laser.business.LaserRuntimeException)6 TopicSession (javax.jms.TopicSession)6 Test (org.junit.Test)6 ConnectionFactory (javax.jms.ConnectionFactory)5 MessageListener (javax.jms.MessageListener)5 Connection (javax.jms.Connection)4 Queue (javax.jms.Queue)3 TopicConnection (javax.jms.TopicConnection)3 ActiveMQConnection (org.apache.activemq.ActiveMQConnection)3 AlarmImpl (cern.laser.business.data.AlarmImpl)2 JmsMessageException (com.axway.ats.action.exceptions.JmsMessageException)2 ManagedSession (com.axway.ats.action.jms.model.sessions.ManagedSession)2