se.kodiak.logging.appenders.amqp.AmqpFactory.java Source code

Java tutorial

Introduction

Here is the source code for se.kodiak.logging.appenders.amqp.AmqpFactory.java

Source

/*
 * Textwall hits you for 101%.
 *     Copyright (C) 2014  Meduzz
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License along
 *     with this program; if not, write to the Free Software Foundation, Inc.,
 *     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package se.kodiak.logging.appenders.amqp;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AmqpFactory implements Factory {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private ConnectionFactory f = new ConnectionFactory();
    private Map<String, Connection> connections = new ConcurrentHashMap<String, Connection>();
    private final ExecutorService service = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());

    protected AmqpFactory() {
    }

    @Override
    public Channel channelFor(String url, String exchange)
            throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException {

        Connection con = this.connections.get(url);

        if (con == null || !con.isOpen()) {
            log.debug("No known connections for URI ({}), creating new.", url);
            f.setUri(url);

            con = f.newConnection();
        }

        Channel channel = con.createChannel();
        setupChannel(channel, exchange);

        return channel;
    }

    @Override
    public Channel channelFor(String host, Integer port, String vhost, String username, String password,
            String exchange)
            throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException {

        ConnectionData data = new ConnectionData();
        data.setHost(host);
        data.setPort(port);
        data.setVhost(vhost);
        data.setUsername(username);
        data.setPassword(password);

        return channelFor(data.getConnectionUrl(), exchange);
    }

    @Override
    public Channel channelFor(ConnectionData data)
            throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException {
        return channelFor(data.getConnectionUrl(), data.getExchange());
    }

    private void setupChannel(Channel channel, String exchange) throws IOException {
        channel.exchangeDeclare(exchange, "topic");
    }

    @Override
    public ExecutorService getExecutorService() {
        return service;
    }
}