I'm trying to figure out why CyclicBarrier is used in SelectionRegistration.
It looks as though the idea is for the onFill method of NbChannelInputStream
to block until the onSelected method reports readiness and unblocks it. Is
CyclicBarrier really the most natural way to model this? Not a
CountDownLatch? Or even just a Condition? How do things recover if there's a
timeout?

Maybe I'm missing something, but the following version of
SelectionRegistration would be a lot easier to reason about:

public class SelectionRegistration {

    // Instead of CyclicBarrier:
    private final Lock readyLock = new ReentrantLock();
    private final Condition ready = readyLock.newCondition();

    public void unblock() {
        ready.signalAll();
    }

    public void block() throws IOException, InterruptedException {
        readyLock.lock();
        try {
            if (!ready.await(IoUtils.TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
                throw new IOException("Selector timeout");
            }
        // Could catch InterruptedException here and restore interrupt
        // instead throwing InterruptedException from this method.
        } finally {
            readyLock.unlock();
        }
    }
}

This code ignores the possibility of spurious wakeup; it isn't hard to adapt
it to deal with that.

--tim

On Thu, Oct 20, 2011 at 5:24 AM, Geoff Bland <[email protected]> wrote:

> I have a very simple test case that I've been trying out for evaluation of
> Restlet. It works well under Restlet 2.0.10 but when I try this on Restlet
> 2.1.rc1 the test fails, the server never responds to the client request.
>
> I also get a "Unable to block the thread at the cyclic barrier" error from
> the server.
>
> Here's the sample code, this works fine with 2.0.10 but fails with 2.1.rc1.
> What am I doing wrong?
>
> MessageExtractorRestlet.java
>
> public class MessageExtractorRestlet {
>
>        // Restlet component
>        private Component component;
>
>        void start() throws Exception {
>
>                // Create a new Restlet component
>                component = new Component();
>
>                // Add a new HTTP server
>                component.getServers().add(Protocol.HTTP, 8182);
>
>                // Create the router
>                final Router router = new
> Router(component.getContext().createChildContext());
>
>                // Capture "message" requests
>                router.attach("/" + MessageResourceServer.getHTTPCommand(),
> MessageResourceServer.class);
>
>                // Attach this application
>                component.getDefaultHost().attach(router);
>
>                // Start the component
>                component.start();
>        }
>
>        void stop() throws Exception {
>
>                if (component != null) {
>                        component.stop();
>                        component = null;
>                }
>        }
>
> }
>
> MessageResource.java
>
> public interface MessageResource {
>
>        @Get
>        public String retrieve(long aSequence);
>
> }
>
> MessageResourceServer.java
>
> public class MessageResourceServer extends ServerResource implements
> MessageResource {
>
>        public static String getHTTPCommand() {
>                return "message";
>        }
>
>        @Override
>        public String retrieve(long aSequence) {
>                return "Here are contents of message " + aSequence;
>        }
> }
>
> MessageExtractorRestletTest.java
>
> public class MessageExtractorRestletTest {
>
>        private static MessageExtractorRestlet testMessageExtractorRestlet;
>
>        @BeforeClass
>        public static void setUpBeforeClass() throws Exception {
>
>                testMessageExtractorRestlet = new MessageExtractorRestlet();
>                testMessageExtractorRestlet.start();
>        }
>
>        @AfterClass
>        public static void tearDownAfterClass() throws Exception {
>                testMessageExtractorRestlet.stop();
>        }
>
>        @Test
>        public void testDefault() {
>
>                ClientResource client_resource = new ClientResource("
> http://localhost:8182/message";);
>
>                MessageResource message_resource =
> client_resource.wrap(MessageResource.class);
>                String message = message_resource.retrieve(10203);
>
>                assertEquals("Here are contents of message 10203", message);
>        }
> }
>
> ------------------------------------------------------
>
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2859507
>

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2859649

Reply via email to