Java tutorial
/* * Copyright 2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.flink.streaming.connectors.jms; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import javax.jms.Topic; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; import javax.jms.TopicPublisher; import javax.jms.TopicSession; import org.apache.flink.configuration.Configuration; import org.apache.flink.streaming.api.functions.sink.RichSinkFunction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.jms.UncategorizedJmsException; import org.springframework.jms.support.JmsUtils; import org.springframework.util.Assert; public abstract class JmsTopicSink<T> extends RichSinkFunction<T> implements InitializingBean { private static final long serialVersionUID = 42L; private static Logger logger = LoggerFactory.getLogger(JmsTopicSink.class); private Topic destination; private TopicConnectionFactory connectionFactory; private TopicConnection connection; private TopicSession session; private TopicPublisher producer; // -------------------------------------------------------------------------- public JmsTopicSink() { } public JmsTopicSink(final TopicConnectionFactory connectionFactory, final Topic destination) { Assert.notNull(connectionFactory, "QueueConnectionFactory must not be null"); Assert.notNull(destination, "Queue must not be null"); this.connectionFactory = connectionFactory; this.destination = destination; } @Override public void open(final Configuration parameters) throws Exception { super.open(parameters); final String username = parameters.getString("jms_username", null); final String password = parameters.getString("jms_password", null); connection = connectionFactory.createTopicConnection(username, password); final String clientId = parameters.getString("jms_client_id", null); if (clientId != null) connection.setClientID(clientId); session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); producer = session.createPublisher(destination); connection.start(); } @Override public void invoke(final T object) throws Exception { try { producer.publish(destination, convert(object, session), Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); } catch (JMSException e) { logger.error("Error sending message to [{}]: {}", destination.getTopicName(), e.getLocalizedMessage(), e); throw new UncategorizedJmsException(e); } } protected abstract Message convert(final T object, final Session session) throws Exception; @Override public void close() throws Exception { super.close(); JmsUtils.closeMessageProducer(producer); JmsUtils.closeSession(session); JmsUtils.closeConnection(connection, true); } public void setDestination(final Topic destination) { this.destination = destination; } public void setConnectionFactory(final TopicConnectionFactory connectionFactory) { this.connectionFactory = connectionFactory; } @Override public void afterPropertiesSet() throws Exception { Assert.notNull(destination, "Destination topic must not be null"); Assert.notNull(connectionFactory, "TopicConnectionFactory must not be null"); } }