summaryrefslogtreecommitdiff
path: root/java/java/cluster/src/org/apache/qpid/server/queue/RemoteQueueProxy.java
blob: 752cf05a8257ac7ce88730945a02c516a2fe36c5 (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
/*
 *
 * 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.queue;

import org.apache.log4j.Logger;
import org.apache.qpid.AMQException;
import org.apache.qpid.framing.AMQBody;
import org.apache.qpid.framing.BasicPublishBody;
import org.apache.qpid.framing.ContentBody;
import org.apache.qpid.framing.ContentHeaderBody;
import org.apache.qpid.server.cluster.ClusteredProtocolSession;
import org.apache.qpid.server.cluster.GroupManager;
import org.apache.qpid.server.cluster.util.LogMessage;
import org.apache.qpid.server.cluster.MemberHandle;
import org.apache.qpid.server.cluster.SimpleSendable;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

/**
 * TODO: separate out an abstract base class from AMQQueue from which this inherits. It does
 * not require all the functionality currently in AMQQueue.
 *
 */
public class RemoteQueueProxy extends AMQQueue
{
    private static final Logger _logger = Logger.getLogger(RemoteQueueProxy.class);
    private final MemberHandle _target;
    private final GroupManager _groupMgr;

    public RemoteQueueProxy(MemberHandle target, GroupManager groupMgr, String name, boolean durable, String owner, boolean autoDelete, QueueRegistry queueRegistry)
            throws AMQException
    {
        super(name, durable, owner, autoDelete, queueRegistry);
        _target = target;
        _groupMgr = groupMgr;
        _groupMgr.addMemberhipChangeListener(new ProxiedQueueCleanup(target, this));
    }

    public RemoteQueueProxy(MemberHandle target, GroupManager groupMgr, String name, boolean durable, String owner, boolean autoDelete, QueueRegistry queueRegistry, Executor asyncDelivery)
            throws AMQException
    {
        super(name, durable, owner, autoDelete, queueRegistry, asyncDelivery);
        _target = target;
        _groupMgr = groupMgr;
        _groupMgr.addMemberhipChangeListener(new ProxiedQueueCleanup(target, this));
    }

    public void deliver(AMQMessage msg) throws NoConsumersException
    {
        if (ClusteredProtocolSession.canRelay(msg, _target))
        {
            try
            {
                _logger.debug(new LogMessage("Relaying {0} to {1}", msg, _target));
                relay(msg);
            }
            catch (NoConsumersException e)
            {
                throw e;
            }
            catch (AMQException e)
            {
                //TODO: sort out exception handling...
                e.printStackTrace();
            }
        }
        else
        {
            _logger.debug(new LogMessage("Cannot relay {0} to {1}", msg, _target));
        }
    }

    void relay(AMQMessage msg) throws AMQException
    {
        BasicPublishBody publish = msg.getPublishBody();
        ContentHeaderBody header = msg.getContentHeaderBody();
        List<ContentBody> bodies = msg.getContentBodies();

        //(i) construct a new publishing block:
        publish.immediate = false;//can't as yet handle the immediate flag in a cluster
        List<AMQBody> parts = new ArrayList<AMQBody>(2 + bodies.size());
        parts.add(publish);
        parts.add(header);
        parts.addAll(bodies);

        //(ii) send this on to the broker for which it is acting as proxy:
        _groupMgr.send(_target, new SimpleSendable(parts));
    }
}