1 package org.sapia.ubik.rmi.server.transport.nio.tcp; 2 3 import java.rmi.RemoteException ; 4 import java.util.Properties ; 5 6 import org.sapia.ubik.net.nio.Dispatcher; 7 import org.sapia.ubik.net.nio.dispatcher.RWPDispatcher; 8 import org.sapia.ubik.rmi.PropUtil; 9 10 import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer; 11 import EDU.oswego.cs.dl.util.concurrent.Channel; 12 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue; 13 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 14 15 75 public class RWPStrategy implements DispatcherStrategy { 76 77 private static final String PROP_PREFIX = "ubik.rmi.transport.nio-tcp.rwp"; 78 private static final String PROP_READ_SUFFIX = "read"; 79 private static final String PROP_WRITE_SUFFIX = "write"; 80 private static final String PROP_PROCESS_SUFFIX = "process"; 81 82 87 public static final String MAX_QUEUE_SIZE = "max-queue-size"; 88 89 93 public static final String MIN_POOL_SIZE = "min-pool-size"; 94 95 99 public static final String MAX_POOL_SIZE = "max-pool-size"; 100 101 106 public static final String KEEP_ALIVE = "keep-alive"; 107 108 112 public static final String ABORT_FULL = "abort-full"; 113 114 120 public Dispatcher newDispatcher(Properties props) throws RemoteException { 121 PropUtil pu = new PropUtil().addProperties(props); 122 pu.addProperties(System.getProperties()); 123 124 PooledExecutor read = createExecutor(PROP_READ_SUFFIX, pu); 125 PooledExecutor write = createExecutor(PROP_WRITE_SUFFIX, pu); 126 PooledExecutor process = createExecutor(PROP_PROCESS_SUFFIX, pu); 127 RWPDispatcher dispatcher = new RWPDispatcher(read, write, process); 128 return dispatcher; 129 } 130 131 private PooledExecutor createExecutor(String suffix, PropUtil props){ 132 int queueSize = props.getIntProperty(prop(suffix, MAX_QUEUE_SIZE), 0); 133 Channel queue; 134 if(queueSize <= 0){ 135 queue = new LinkedQueue(); 136 } 137 else{ 138 queue = new BoundedBuffer(queueSize); 139 } 140 141 PooledExecutor exec = new PooledExecutor(queue); 142 143 int minPoolSize = props.getIntProperty(prop(suffix, MIN_POOL_SIZE), 0); 144 if(minPoolSize > 0){ 145 exec.setMinimumPoolSize(minPoolSize); 146 } 147 148 int maxPoolSize = props.getIntProperty(prop(suffix, MAX_POOL_SIZE), 0); 149 if(minPoolSize > 0){ 150 exec.setMaximumPoolSize(maxPoolSize); 151 } 152 153 long keepAlive = props.getLongProperty(prop(suffix, KEEP_ALIVE), -1); 154 exec.setKeepAliveTime(keepAlive); 155 156 boolean abortFull = props.getBooleanProperty(prop(suffix, ABORT_FULL), true); 157 if(abortFull && queue instanceof BoundedBuffer){ 158 exec.abortWhenBlocked(); 159 } 160 return exec; 161 } 162 163 private String prop(String suffix, String propType){ 164 return new StringBuffer ().append(PROP_PREFIX).append('.').append(suffix).append('.').append(propType).toString(); 165 } 166 167 } 168 | Popular Tags |