summaryrefslogtreecommitdiff
path: root/java/cluster/src/main/java/org/apache/qpid/server/cluster/handler/ReplicatingHandler.java
blob: 888fa4e426b96292dcc9443421aef02f0c5faef0 (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
/*
 *
 * 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.util.LogMessage;
import org.apache.qpid.server.cluster.*;
import org.apache.qpid.server.cluster.policy.StandardPolicies;
import org.apache.qpid.server.exchange.ExchangeRegistry;
import org.apache.qpid.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 org.apache.qpid.server.virtualhost.VirtualHost;

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 stateManager, AMQMethodEvent<A> evt) throws AMQException
    {
        AMQProtocolSession session = stateManager.getProtocolSession();
        VirtualHost virtualHost = session.getVirtualHost();
        ExchangeRegistry exchangeRegistry = virtualHost.getExchangeRegistry();
        QueueRegistry queueRegistry = virtualHost.getQueueRegistry();

        local(stateManager, evt);
        _logger.debug(new LogMessage("Handled {0} locally", evt.getMethod()));
    }

    protected void client(AMQStateManager stateMgr, AMQMethodEvent<A> evt) throws AMQException
    {
        replicate(stateMgr, evt);
    }

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

    protected void local(AMQStateManager stateMgr, AMQMethodEvent<A> evt) throws AMQException
    {
        _base.methodReceived(stateMgr,  evt);
    }

    private class Callback implements GroupResponseHandler
    {
        private final AMQStateManager _stateMgr;
        private final AMQMethodEvent<A> _evt;

        Callback(AMQStateManager stateMgr, AMQMethodEvent<A> evt)
        {
            _stateMgr = stateMgr;
            _evt = evt;
        }

        public void response(List<AMQMethodBody> responses, List<Member> members)
        {
            try
            {
                local(_stateMgr, _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);
            }
        }
    }
}