use async dispatch for vm:// transport by default

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@515999 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Davies 2007-03-08 10:22:22 +00:00
parent a08a4a84c7
commit da13d596a5
4 changed files with 79 additions and 49 deletions

View File

@ -58,9 +58,10 @@ class DedicatedTaskRunner implements TaskRunner {
/** /**
* shut down the task * shut down the task
* @param timeout
* @throws InterruptedException * @throws InterruptedException
*/ */
public void shutdown() throws InterruptedException{ public void shutdown(long timeout) throws InterruptedException{
synchronized(mutex){ synchronized(mutex){
shutdown=true; shutdown=true;
pending=true; pending=true;
@ -68,10 +69,18 @@ class DedicatedTaskRunner implements TaskRunner {
// Wait till the thread stops. // Wait till the thread stops.
if(!threadTerminated){ if(!threadTerminated){
mutex.wait(); mutex.wait(timeout);
} }
} }
} }
/**
* shut down the task
* @throws InterruptedException
*/
public void shutdown() throws InterruptedException{
shutdown(0);
}
private void runTask() { private void runTask() {

View File

@ -77,7 +77,7 @@ class PooledTaskRunner implements TaskRunner {
* shut down the task * shut down the task
* @throws InterruptedException * @throws InterruptedException
*/ */
public void shutdown() throws InterruptedException{ public void shutdown(long timeout) throws InterruptedException{
synchronized(runable){ synchronized(runable){
shutdown=true; shutdown=true;
//the check on the thread is done //the check on the thread is done
@ -85,13 +85,17 @@ class PooledTaskRunner implements TaskRunner {
//shutDown() being called, which would wait forever //shutDown() being called, which would wait forever
//waiting for iterating to finish //waiting for iterating to finish
if(runningThread!=Thread.currentThread()){ if(runningThread!=Thread.currentThread()){
while(iterating==true){ if(iterating==true){
runable.wait(); runable.wait(timeout);
} }
} }
} }
} }
public void shutdown() throws InterruptedException {
shutdown(0);
}
private void runTask() { private void runTask() {
synchronized (runable) { synchronized (runable) {

View File

@ -25,4 +25,5 @@ package org.apache.activemq.thread;
public interface TaskRunner { public interface TaskRunner {
public abstract void wakeup() throws InterruptedException; public abstract void wakeup() throws InterruptedException;
public abstract void shutdown() throws InterruptedException; public abstract void shutdown() throws InterruptedException;
public abstract void shutdown(long timeout) throws InterruptedException;
} }

View File

@ -21,6 +21,7 @@ import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import org.apache.activemq.command.Command; import org.apache.activemq.command.Command;
import org.apache.activemq.thread.Task; import org.apache.activemq.thread.Task;
@ -50,26 +51,31 @@ public class VMTransport implements Transport,Task{
protected boolean disposed; protected boolean disposed;
protected boolean marshal; protected boolean marshal;
protected boolean network; protected boolean network;
protected boolean async=false; protected boolean async=true;
protected boolean started=false; protected AtomicBoolean started=new AtomicBoolean();
protected int asyncQueueDepth=2000; protected int asyncQueueDepth=2000;
protected List prePeerSetQueue=Collections.synchronizedList(new LinkedList()); protected List prePeerSetQueue=Collections.synchronizedList(new LinkedList());
protected LinkedBlockingQueue messageQueue=null; protected LinkedBlockingQueue messageQueue=null;
protected final URI location; protected final URI location;
protected final long id; protected final long id;
private TaskRunner taskRunner; private TaskRunner taskRunner;
private final Object mutex=new Object();
public VMTransport(URI location){ public VMTransport(URI location){
this.location=location; this.location=location;
this.id=nextId.getAndIncrement(); this.id=nextId.getAndIncrement();
} }
synchronized public VMTransport getPeer(){ public VMTransport getPeer(){
return peer; synchronized(mutex){
return peer;
}
} }
synchronized public void setPeer(VMTransport peer){ public void setPeer(VMTransport peer){
this.peer=peer; synchronized(mutex){
this.peer=peer;
}
} }
public void oneway(Object command) throws IOException{ public void oneway(Object command) throws IOException{
@ -99,10 +105,12 @@ public class VMTransport implements Transport,Task{
} }
} }
protected synchronized void asyncOneWay(Object command) throws IOException{ protected void asyncOneWay(Object command) throws IOException{
try{ try{
if(messageQueue==null){ synchronized(mutex){
messageQueue=new LinkedBlockingQueue(this.asyncQueueDepth); if(messageQueue==null){
messageQueue=new LinkedBlockingQueue(this.asyncQueueDepth);
}
} }
messageQueue.put(command); messageQueue.put(command);
wakeup(); wakeup();
@ -124,40 +132,46 @@ public class VMTransport implements Transport,Task{
throw new AssertionError("Unsupported Method"); throw new AssertionError("Unsupported Method");
} }
public synchronized TransportListener getTransportListener(){ public TransportListener getTransportListener(){
return transportListener; synchronized(mutex){
return transportListener;
}
} }
synchronized public void setTransportListener(TransportListener commandListener){ public void setTransportListener(TransportListener commandListener){
this.transportListener=commandListener; synchronized(mutex){
this.transportListener=commandListener;
}
wakeup(); wakeup();
peer.wakeup(); peer.wakeup();
} }
public synchronized void start() throws Exception{ public void start() throws Exception{
started=true; if(started.compareAndSet(false,true)){
if(transportListener==null) if(transportListener==null)
throw new IOException("TransportListener not set."); throw new IOException("TransportListener not set.");
if(!async){ if(!async){
for(Iterator iter=prePeerSetQueue.iterator();iter.hasNext();){ for(Iterator iter=prePeerSetQueue.iterator();iter.hasNext();){
Command command=(Command)iter.next(); Command command=(Command)iter.next();
transportListener.onCommand(command); transportListener.onCommand(command);
iter.remove(); iter.remove();
}
}else{
peer.wakeup();
wakeup();
} }
}else{
peer.wakeup();
wakeup();
} }
} }
public synchronized void stop() throws Exception{ public void stop() throws Exception{
started=false; if(started.compareAndSet(true,false)){
if(!disposed){ if(!disposed){
disposed=true; disposed=true;
} }
if(taskRunner!=null){ if(taskRunner!=null){
taskRunner.shutdown(); taskRunner.shutdown(1000);
taskRunner=null; taskRunner=null;
}
} }
} }
@ -201,16 +215,16 @@ public class VMTransport implements Transport,Task{
public boolean iterate(){ public boolean iterate(){
final TransportListener tl=peer.transportListener; final TransportListener tl=peer.transportListener;
Command command=null; Command command=null;
// if(!disposed && !messageQueue.isEmpty()&&!peer.disposed&&tl!=null){ synchronized(mutex){
synchronized(this){ if(messageQueue!=null&&!disposed&&!peer.disposed&&tl!=null&&!messageQueue.isEmpty()){
if(messageQueue!=null&&!disposed&&!peer.disposed&&tl!=null &&!messageQueue.isEmpty()){
command=(Command)messageQueue.poll(); command=(Command)messageQueue.poll();
if (command != null) {
tl.onCommand(command);
}
} }
} }
return messageQueue!=null&&!messageQueue.isEmpty()&&!peer.disposed; if(tl!=null&&command!=null){
tl.onCommand(command);
}
boolean result=messageQueue!=null&&!messageQueue.isEmpty()&&!peer.disposed;
return result;
} }
/** /**
@ -241,10 +255,12 @@ public class VMTransport implements Transport,Task{
this.asyncQueueDepth=asyncQueueDepth; this.asyncQueueDepth=asyncQueueDepth;
} }
protected synchronized void wakeup(){ protected void wakeup(){
if(async){ if(async){
if(taskRunner==null){ synchronized(mutex){
taskRunner=taskRunnerFactory.createTaskRunner(this,"VMTransport: "+toString()); if(taskRunner==null){
taskRunner=taskRunnerFactory.createTaskRunner(this,"VMTransport: "+toString());
}
} }
try{ try{
taskRunner.wakeup(); taskRunner.wakeup();