1 11 12 package org.jivesoftware.messenger; 13 14 import java.util.concurrent.LinkedBlockingQueue ; 15 import java.util.concurrent.ThreadPoolExecutor ; 16 import java.util.concurrent.TimeUnit ; 17 import org.jivesoftware.util.LocaleUtils; 18 import org.jivesoftware.util.Log; 19 import org.xmpp.packet.Packet; 20 21 40 public class Channel<T extends Packet> { 41 42 private String name; 43 private ChannelHandler channelHandler; 44 45 ThreadPoolExecutor executor; 46 47 53 public Channel(String name, ChannelHandler<T> channelHandler) { 54 this.name = name; 55 this.channelHandler = channelHandler; 56 57 executor = new ThreadPoolExecutor (1, 8, 15, TimeUnit.SECONDS, new LinkedBlockingQueue ()); 58 } 59 60 65 public String getName() { 66 return name; 67 } 68 69 76 public void add(final T packet) { 77 Runnable r = new Runnable () { 78 public void run() { 79 try { 80 channelHandler.process(packet); 81 } 82 catch (Exception e) { 83 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 84 85 try { 86 Session session = SessionManager.getInstance().getSession(packet.getFrom()); 87 session.getConnection().close(); 88 } 89 catch (Exception e1) { 90 Log.error(e1); 91 } 92 } 93 } 94 }; 95 executor.execute(r); 96 } 97 98 104 public boolean isRunning() { 105 return !executor.isShutdown(); 106 } 107 108 112 public void start() { 113 114 } 115 116 120 public synchronized void stop() { 121 executor.shutdown(); 122 } 123 124 130 public int getThreadCount() { 131 return executor.getPoolSize(); 132 } 133 134 142 public int getMinThreadCount() { 143 return executor.getCorePoolSize(); 144 } 145 146 154 public void setMinThreadCount(int minThreadCount) { 155 executor.setCorePoolSize(minThreadCount); 156 } 157 158 166 public int getMaxThreadCount() { 167 return executor.getMaximumPoolSize(); 168 } 169 170 178 public void setMaxThreadCount(int maxThreadCount) { 179 executor.setMaximumPoolSize(maxThreadCount); 180 } 181 182 188 public int getQueueSize() { 189 return executor.getQueue().size(); 190 } 191 } | Popular Tags |