KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > reliable > UnreliableCommandDatagramChannel


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.activemq.transport.reliable;
18
19 import org.apache.activemq.openwire.OpenWireFormat;
20 import org.apache.activemq.transport.udp.ByteBufferPool;
21 import org.apache.activemq.transport.udp.CommandDatagramChannel;
22 import org.apache.activemq.transport.udp.DatagramHeaderMarshaller;
23 import org.apache.activemq.transport.udp.UdpTransport;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import java.io.IOException JavaDoc;
28 import java.net.SocketAddress JavaDoc;
29 import java.nio.ByteBuffer JavaDoc;
30 import java.nio.channels.DatagramChannel JavaDoc;
31
32 /**
33  *
34  * @version $Revision: $
35  */

36 public class UnreliableCommandDatagramChannel extends CommandDatagramChannel {
37
38     private static final Log log = LogFactory.getLog(UnreliableCommandDatagramChannel.class);
39
40     private DropCommandStrategy dropCommandStrategy;
41
42     public UnreliableCommandDatagramChannel(UdpTransport transport, OpenWireFormat wireFormat, int datagramSize,
43             SocketAddress JavaDoc targetAddress, DatagramHeaderMarshaller headerMarshaller, ReplayBuffer replayBuffer, DatagramChannel channel,
44             ByteBufferPool bufferPool, DropCommandStrategy strategy) {
45         super(transport, wireFormat, datagramSize, targetAddress, headerMarshaller, channel, bufferPool);
46         this.dropCommandStrategy = strategy;
47     }
48
49     protected void sendWriteBuffer(int commandId, SocketAddress JavaDoc address, ByteBuffer JavaDoc writeBuffer, boolean redelivery) throws IOException JavaDoc {
50         if (dropCommandStrategy.shouldDropCommand(commandId, address, redelivery)) {
51             writeBuffer.flip();
52             log.info("Dropping datagram with command: " + commandId);
53             
54             // lets still add it to the replay buffer though!
55
getReplayBuffer().addBuffer(commandId, writeBuffer);
56         }
57         else {
58             super.sendWriteBuffer(commandId, address, writeBuffer, redelivery);
59         }
60     }
61 }
62
Popular Tags