KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > http > BlockingQueueTransport


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

18 package org.apache.activemq.transport.http;
19
20 import java.util.Queue JavaDoc;
21 import java.util.concurrent.BlockingQueue JavaDoc;
22 import java.util.concurrent.TimeUnit JavaDoc;
23
24 import org.apache.activemq.transport.TransportSupport;
25 import org.apache.activemq.util.ServiceStopper;
26
27 import java.io.IOException JavaDoc;
28
29 /**
30  * A server side HTTP based TransportChannel which processes incoming packets
31  * and adds outgoing packets onto a {@link Queue} so that they can be dispatched
32  * by the HTTP GET requests from the client.
33  *
34  * @version $Revision$
35  */

36 public class BlockingQueueTransport extends TransportSupport {
37     public static final long MAX_TIMEOUT = 30000L;
38
39     private BlockingQueue JavaDoc queue;
40
41     public BlockingQueueTransport(BlockingQueue JavaDoc channel) {
42         this.queue = channel;
43     }
44
45     public BlockingQueue JavaDoc getQueue() {
46         return queue;
47     }
48
49     public void oneway(Object JavaDoc command) throws IOException JavaDoc {
50         try {
51             boolean success = queue.offer(command, MAX_TIMEOUT, TimeUnit.MILLISECONDS);
52             if (!success)
53                 throw new IOException JavaDoc("Fail to add to BlockingQueue. Add timed out after " + MAX_TIMEOUT + "ms: size=" + queue.size());
54         } catch (InterruptedException JavaDoc e) {
55             throw new IOException JavaDoc("Fail to add to BlockingQueue. Interrupted while waiting for space: size=" + queue.size());
56         }
57     }
58
59     
60     public String JavaDoc getRemoteAddress() {
61         return "blockingQueue";
62     }
63
64     protected void doStart() throws Exception JavaDoc {
65     }
66
67     protected void doStop(ServiceStopper stopper) throws Exception JavaDoc {
68     }
69 }
70
Popular Tags