001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.transport.nio; 018 019import java.nio.channels.CancelledKeyException; 020import java.nio.channels.ClosedChannelException; 021import java.nio.channels.SelectionKey; 022import java.nio.channels.spi.AbstractSelectableChannel; 023import java.util.concurrent.atomic.AtomicBoolean; 024 025import org.apache.activemq.transport.nio.SelectorManager.Listener; 026 027/** 028 * 029 */ 030public final class SelectorSelection { 031 032 private final SelectorWorker worker; 033 private final Listener listener; 034 private int interest; 035 private SelectionKey key; 036 private final AtomicBoolean closed = new AtomicBoolean(); 037 038 public SelectorSelection(final SelectorWorker worker, final AbstractSelectableChannel selectable, Listener listener) throws ClosedChannelException { 039 this.worker = worker; 040 this.listener = listener; 041 worker.addIoTask(new Runnable() { 042 @Override 043 public void run() { 044 try { 045 SelectorSelection.this.key = selectable.register(worker.selector, 0, SelectorSelection.this); 046 } catch (Exception e) { 047 e.printStackTrace(); 048 } 049 } 050 }); 051 } 052 053 public void setInterestOps(int ops) { 054 interest = ops; 055 } 056 057 public void enable() { 058 worker.addIoTask(new Runnable() { 059 @Override 060 public void run() { 061 try { 062 key.interestOps(interest); 063 } catch (CancelledKeyException e) { 064 } 065 } 066 }); 067 } 068 069 public void disable() { 070 worker.addIoTask(new Runnable() { 071 @Override 072 public void run() { 073 try { 074 key.interestOps(0); 075 } catch (CancelledKeyException e) { 076 } 077 } 078 }); 079 } 080 081 public void close() { 082 if (closed.compareAndSet(false, true)) { 083 worker.addIoTask(new Runnable() { 084 @Override 085 public void run() { 086 try { 087 key.cancel(); 088 } catch (CancelledKeyException e) { 089 } 090 worker.release(); 091 } 092 }); 093 } 094 } 095 096 public void onSelect() { 097 listener.onSelect(this); 098 } 099 100 public void onError(Throwable e) { 101 listener.onError(this, e); 102 } 103}