|
ActiveMQ example source code file (SelectorSelection.java)
The ActiveMQ SelectorSelection.java source code/** * 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.activemq.transport.nio; import java.nio.channels.CancelledKeyException; import java.nio.channels.ClosedChannelException; import java.nio.channels.SelectionKey; import java.nio.channels.SocketChannel; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.activemq.transport.nio.SelectorManager.Listener; /** * @author chirino */ public final class SelectorSelection { private final SelectorWorker worker; private final Listener listener; private int interest; private SelectionKey key; private AtomicBoolean closed = new AtomicBoolean(); public SelectorSelection(final SelectorWorker worker, final SocketChannel socketChannel, Listener listener) throws ClosedChannelException { this.worker = worker; this.listener = listener; worker.addIoTask(new Runnable() { public void run() { try { SelectorSelection.this.key = socketChannel.register(worker.selector, 0, SelectorSelection.this); } catch (Exception e) { e.printStackTrace(); } } }); } public void setInterestOps(int ops) { interest = ops; } public void enable() { worker.addIoTask(new Runnable() { public void run() { try { key.interestOps(interest); } catch (CancelledKeyException e) { } } }); } public void disable() { worker.addIoTask(new Runnable() { public void run() { try { key.interestOps(0); } catch (CancelledKeyException e) { } } }); } public void close() { // guard against multiple closes. if( closed.compareAndSet(false, true) ) { worker.addIoTask(new Runnable() { public void run() { try { key.cancel(); } catch (CancelledKeyException e) { } worker.release(); } }); } } public void onSelect() { listener.onSelect(this); } public void onError(Throwable e) { listener.onError(this, e); } } Other ActiveMQ examples (source code examples)Here is a short list of links related to this ActiveMQ SelectorSelection.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.