summaryrefslogtreecommitdiff
path: root/cluster/src/org/apache/qpid/server/cluster/Main.java
blob: 3eeddd7b4e09af7518ed6d20735e47a22aad0852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.qpid.server.cluster;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.log4j.Logger;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
import org.apache.mina.transport.socket.nio.SocketSessionConfig;
import org.apache.qpid.pool.ReadWriteThreadModel;
import org.apache.qpid.server.registry.ApplicationRegistry;
import org.apache.qpid.server.registry.ConfigurationFileApplicationRegistry;
import org.apache.qpid.server.transport.ConnectorConfiguration;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
 * TODO: This is a cut-and-paste from the original broker Main class. Would be preferrable
 * to make that class more reuseable to avoid all this duplication.
 *
 */
public class Main extends org.apache.qpid.server.Main
{
    private static final Logger _logger = Logger.getLogger(Main.class);

    protected Main(String[] args)
    {
        super(args);
    }

    protected void setOptions(Options otions)
    {
        super.setOptions(options);

        //extensions:
        Option join = OptionBuilder.withArgName("join").hasArg().withDescription("Join the specified cluster member. Overrides any value in the config file").
                withLongOpt("join").create("j");
        options.addOption(join);
    }

    protected void bind(int port, ConnectorConfiguration connectorConfig)
    {
        try
        {
            IoAcceptor acceptor = new SocketAcceptor();
            SocketAcceptorConfig sconfig = (SocketAcceptorConfig) acceptor.getDefaultConfig();
            SocketSessionConfig sc = (SocketSessionConfig) sconfig.getSessionConfig();

            sc.setReceiveBufferSize(connectorConfig.socketReceiveBufferSize);
            sc.setSendBufferSize(connectorConfig.socketWriteBuferSize);
            sc.setTcpNoDelay(true);

            // if we do not use the executor pool threading model we get the default leader follower
            // implementation provided by MINA
            if (connectorConfig.enableExecutorPool)
            {
                sconfig.setThreadModel(new ReadWriteThreadModel());
            }

            String host = InetAddress.getLocalHost().getHostName();
            ClusteredProtocolHandler handler = new ClusteredProtocolHandler(new InetSocketAddress(host, port));
            if (connectorConfig.enableNonSSL)
            {
                acceptor.bind(new InetSocketAddress(port), handler, sconfig);
                _logger.info("Qpid.AMQP listening on non-SSL port " + port);
                handler.connect(commandLine.getOptionValue("j"));
            }

            if (connectorConfig.enableSSL)
            {
                ClusteredProtocolHandler sslHandler = new ClusteredProtocolHandler(handler);
                sslHandler.setUseSSL(true);
                acceptor.bind(new InetSocketAddress(connectorConfig.sslPort), handler, sconfig);
                _logger.info("Qpid.AMQP listening on SSL port " + connectorConfig.sslPort);
            }
        }
        catch (IOException e)
        {
            _logger.error("Unable to bind service to registry: " + e, e);
        }
        catch (Exception e)
        {
            _logger.error("Unable to connect to cluster: " + e, e);
        }
    }

    public static void main(String[] args)
    {
        new Main(args);
    }
}