Skip to content

Commit

Permalink
Reformat some classes
Browse files Browse the repository at this point in the history
  • Loading branch information
coderplay committed Jul 16, 2015
1 parent 1249ccf commit 3ee999d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@

/**
* Help for threadpool to set thread name
*
*/
public class NamedThreadFactory implements ThreadFactory {

static final AtomicInteger poolNumber = new AtomicInteger(1);

final AtomicInteger threadNumber = new AtomicInteger(1);
final ThreadGroup group;
final String namePrefix;
final boolean isDaemon;

public NamedThreadFactory() {
this("pool");
}
public NamedThreadFactory(String name) {
this(name, false);
}
public NamedThreadFactory(String preffix, boolean daemon) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread()
.getThreadGroup();
namePrefix = preffix + "-" + poolNumber.getAndIncrement() + "-thread-";
isDaemon = daemon;
}


public Thread newThread(Runnable r) {
Thread t = new Thread(group, r, namePrefix
+ threadNumber.getAndIncrement(), 0);
t.setDaemon(isDaemon);
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
static final AtomicInteger poolNumber = new AtomicInteger(1);

final AtomicInteger threadNumber = new AtomicInteger(1);
final ThreadGroup group;
final String namePrefix;
final boolean isDaemon;

public NamedThreadFactory() {
this("pool");
}

public NamedThreadFactory(String name) {
this(name, false);
}

public NamedThreadFactory(String preffix, boolean daemon) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread()
.getThreadGroup();
namePrefix = preffix + "-" + poolNumber.getAndIncrement() + "-thread-";
isDaemon = daemon;
}


public Thread newThread(Runnable r) {
Thread t = new Thread(group, r, namePrefix
+ threadNumber.getAndIncrement(), 0);
t.setDaemon(isDaemon);
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,33 @@

import info.minzhou.lightning.rpc.RequestWrapper;
import info.minzhou.lightning.rpc.ResponseWrapper;

/**
* Direct Call RPC Server Handler
*
*/
public class SimpleProcessorServerHandler implements ServerHandler{

private static final Log LOGGER = LogFactory.getLog(SimpleProcessorServerHandler.class);

private Map<String, ServerProcessor> processors = new ConcurrentHashMap<String, ServerProcessor>();

public void registerProcessor(String instanceName,Object instance){
processors.put(instanceName, (ServerProcessor)instance);
}

public ResponseWrapper handleRequest(final RequestWrapper request){
ResponseWrapper responseWrapper = new ResponseWrapper(request.getId(),request.getCodecType(),request.getProtocolType());
try{
String argType = null;
if(request.getArgTypes() != null && request.getArgTypes()[0] != null){
argType = new String(request.getArgTypes()[0]);
}
Object requestObject = Codecs.getDecoder(request.getCodecType()).decode(argType,(byte[])request.getMessage());
responseWrapper.setResponse(processors.get(requestObject.getClass().getName()).handle(requestObject));
}
catch(Exception e){
LOGGER.error("server direct call handler to handle request error",e);
responseWrapper.setException(e);
}
return responseWrapper;
}
public class SimpleProcessorServerHandler implements ServerHandler {

private static final Log LOGGER = LogFactory.getLog(SimpleProcessorServerHandler.class);

private Map<String, ServerProcessor> processors = new ConcurrentHashMap<String, ServerProcessor>();

public void registerProcessor(String instanceName, Object instance) {
processors.put(instanceName, (ServerProcessor) instance);
}

public ResponseWrapper handleRequest(final RequestWrapper request) {
ResponseWrapper responseWrapper = new ResponseWrapper(request.getId(), request.getCodecType(), request.getProtocolType());
try {
String argType = null;
if (request.getArgTypes() != null && request.getArgTypes()[0] != null) {
argType = new String(request.getArgTypes()[0]);
}
Object requestObject = Codecs.getDecoder(request.getCodecType()).decode(argType, (byte[]) request.getMessage());
responseWrapper.setResponse(processors.get(requestObject.getClass().getName()).handle(requestObject));
} catch (Exception e) {
LOGGER.error("server direct call handler to handle request error", e);
responseWrapper.setException(e);
}
return responseWrapper;
}
}

0 comments on commit 3ee999d

Please sign in to comment.