summaryrefslogtreecommitdiff
path: root/cluster/src/org/apache/qpid/server/cluster/handler/ReplicatingHandler.java
blob: db340c6a61e1941c43544fe775ba44d7c50173f0 (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
121
122
123
124
125
126
127
128
129
130
/*
 *
 * 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.handler;

import org.apache.log4j.Logger;
import org.apache.qpid.AMQException;
import org.apache.qpid.framing.AMQMethodBody;
import org.apache.qpid.server.cluster.BroadcastPolicy;
import org.apache.qpid.server.cluster.ClusteredProtocolSession;
import org.apache.qpid.server.cluster.GroupManager;
import org.apache.qpid.server.cluster.GroupResponseHandler;
import org.apache.qpid.server.cluster.util.LogMessage;
import org.apache.qpid.server.cluster.Member;
import org.apache.qpid.server.cluster.SimpleSendable;
import org.apache.qpid.server.cluster.policy.StandardPolicies;
import org.apache.qpid.server.exchange.ExchangeRegistry;
import org.apache.qpid.server.protocol.AMQMethodEvent;
import org.apache.qpid.server.protocol.AMQProtocolSession;
import org.apache.qpid.server.queue.QueueRegistry;
import org.apache.qpid.server.state.AMQStateManager;
import org.apache.qpid.server.state.StateAwareMethodListener;

import java.util.List;

/**
 * Basic template for handling methods that should be broadcast to the group and
 * processed locally after 'completion' of this broadcast.
 *
 */
class ReplicatingHandler<A extends AMQMethodBody> extends ClusterMethodHandler<A> implements StandardPolicies
{
    protected static final Logger _logger = Logger.getLogger(ReplicatingHandler.class);

    private final StateAwareMethodListener<A> _base;
    private final GroupManager _groupMgr;
    private final BroadcastPolicy _policy;

    ReplicatingHandler(GroupManager groupMgr, StateAwareMethodListener<A> base)
    {
        this(groupMgr, base, null);
    }

    ReplicatingHandler(GroupManager groupMgr, StateAwareMethodListener<A> base, BroadcastPolicy policy)
    {
        _groupMgr = groupMgr;
        _base = base;
        _policy = policy;
    }

    protected void peer(AMQStateManager stateMgr, QueueRegistry queues, ExchangeRegistry exchanges, AMQProtocolSession session, AMQMethodEvent<A> evt) throws AMQException
    {
        local(stateMgr, queues, exchanges, session, evt);
        _logger.debug(new LogMessage("Handled {0} locally", evt.getMethod()));
    }

    protected void client(AMQStateManager stateMgr, QueueRegistry queues, ExchangeRegistry exchanges, AMQProtocolSession session, AMQMethodEvent<A> evt) throws AMQException
    {
        replicate(stateMgr, queues, exchanges, session, evt);
    }

    protected void replicate(AMQStateManager stateMgr, QueueRegistry queues, ExchangeRegistry exchanges, AMQProtocolSession session, AMQMethodEvent<A> evt) throws AMQException
    {
        if (_policy == null)
        {
            //asynch delivery
            _groupMgr.broadcast(new SimpleSendable(evt.getMethod()));
            local(stateMgr, queues, exchanges, session, evt);
        }
        else
        {
            Callback callback = new Callback(stateMgr, queues, exchanges, session, evt);
            _groupMgr.broadcast(new SimpleSendable(evt.getMethod()), _policy, callback);
        }
        _logger.debug(new LogMessage("Replicated {0} to peers", evt.getMethod()));
    }

    protected void local(AMQStateManager stateMgr, QueueRegistry queues, ExchangeRegistry exchanges, AMQProtocolSession session, AMQMethodEvent<A> evt) throws AMQException
    {
        _base.methodReceived(stateMgr, queues, exchanges, session, evt);
    }

    private class Callback implements GroupResponseHandler
    {
        private final AMQStateManager _stateMgr;
        private final QueueRegistry _queues;
        private final ExchangeRegistry _exchanges;
        private final AMQProtocolSession _session;
        private final AMQMethodEvent<A> _evt;

        Callback(AMQStateManager stateMgr, QueueRegistry queues, ExchangeRegistry exchanges, AMQProtocolSession session, AMQMethodEvent<A> evt)
        {
            _stateMgr = stateMgr;
            _queues = queues;
            _exchanges = exchanges;
            _session = session;
            _evt = evt;
        }

        public void response(List<AMQMethodBody> responses, List<Member> members)
        {
            try
            {
                local(_stateMgr, _queues, _exchanges, _session, _evt);
                _logger.debug(new LogMessage("Handled {0} locally, in response to completion of replication", _evt.getMethod()));
            }
            catch (AMQException e)
            {
                _logger.error(new LogMessage("Error handling {0}:{1}", _evt.getMethod(), e), e);
            }
        }
    }
}