mirror of https://github.com/apache/activemq.git
small op
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@509728 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
76c466ddef
commit
32a4c47204
|
@ -1,125 +1,122 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
|
||||||
* this work for additional information regarding copyright ownership.
|
* to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* License. You may obtain a copy of the License at
|
||||||
* (the "License"); you may not use this file except in compliance with
|
*
|
||||||
* the License. You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* specific language governing permissions and limitations under the License.
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.apache.activemq.transport;
|
package org.apache.activemq.transport;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
import org.apache.activemq.command.Command;
|
import org.apache.activemq.command.Command;
|
||||||
import org.apache.activemq.command.ExceptionResponse;
|
import org.apache.activemq.command.ExceptionResponse;
|
||||||
import org.apache.activemq.command.Response;
|
import org.apache.activemq.command.Response;
|
||||||
import org.apache.activemq.util.IntSequenceGenerator;
|
import org.apache.activemq.util.IntSequenceGenerator;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds the incrementing sequence number to commands along with performing the corelation of
|
* Adds the incrementing sequence number to commands along with performing the corelation of responses to requests to
|
||||||
* responses to requests to create a blocking request-response semantics.
|
* create a blocking request-response semantics.
|
||||||
*
|
*
|
||||||
* @version $Revision: 1.4 $
|
* @version $Revision: 1.4 $
|
||||||
*/
|
*/
|
||||||
public class ResponseCorrelator extends TransportFilter {
|
public class ResponseCorrelator extends TransportFilter{
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(ResponseCorrelator.class);
|
private static final Log log=LogFactory.getLog(ResponseCorrelator.class);
|
||||||
|
private final Map requestMap=new HashMap();
|
||||||
private final ConcurrentHashMap requestMap = new ConcurrentHashMap();
|
|
||||||
private IntSequenceGenerator sequenceGenerator;
|
private IntSequenceGenerator sequenceGenerator;
|
||||||
private final boolean debug = log.isDebugEnabled();
|
private final boolean debug=log.isDebugEnabled();
|
||||||
public ResponseCorrelator(Transport next) {
|
|
||||||
this(next, new IntSequenceGenerator());
|
public ResponseCorrelator(Transport next){
|
||||||
}
|
this(next,new IntSequenceGenerator());
|
||||||
|
|
||||||
public ResponseCorrelator(Transport next, IntSequenceGenerator sequenceGenerator) {
|
|
||||||
super(next);
|
|
||||||
this.sequenceGenerator = sequenceGenerator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void oneway(Object o) throws IOException {
|
public ResponseCorrelator(Transport next,IntSequenceGenerator sequenceGenerator){
|
||||||
Command command = (Command) o;
|
super(next);
|
||||||
|
this.sequenceGenerator=sequenceGenerator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void oneway(Object o) throws IOException{
|
||||||
|
Command command=(Command)o;
|
||||||
command.setCommandId(sequenceGenerator.getNextSequenceId());
|
command.setCommandId(sequenceGenerator.getNextSequenceId());
|
||||||
command.setResponseRequired(false);
|
command.setResponseRequired(false);
|
||||||
next.oneway(command);
|
next.oneway(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FutureResponse asyncRequest(Object o, ResponseCallback responseCallback) throws IOException {
|
public FutureResponse asyncRequest(Object o,ResponseCallback responseCallback) throws IOException{
|
||||||
Command command = (Command) o;
|
Command command=(Command)o;
|
||||||
command.setCommandId(sequenceGenerator.getNextSequenceId());
|
command.setCommandId(sequenceGenerator.getNextSequenceId());
|
||||||
command.setResponseRequired(true);
|
command.setResponseRequired(true);
|
||||||
FutureResponse future = new FutureResponse(responseCallback);
|
FutureResponse future=new FutureResponse(responseCallback);
|
||||||
requestMap.put(new Integer(command.getCommandId()), future);
|
synchronized(requestMap){
|
||||||
|
requestMap.put(new Integer(command.getCommandId()),future);
|
||||||
|
}
|
||||||
next.oneway(command);
|
next.oneway(command);
|
||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object request(Object command) throws IOException {
|
public Object request(Object command) throws IOException{
|
||||||
FutureResponse response = asyncRequest(command, null);
|
FutureResponse response=asyncRequest(command,null);
|
||||||
return response.getResult();
|
return response.getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object request(Object command,int timeout) throws IOException {
|
public Object request(Object command,int timeout) throws IOException{
|
||||||
FutureResponse response = asyncRequest(command, null);
|
FutureResponse response=asyncRequest(command,null);
|
||||||
return response.getResult(timeout);
|
return response.getResult(timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onCommand(Object o) {
|
public void onCommand(Object o){
|
||||||
Command command = (Command) o;
|
Command command=(Command)o;
|
||||||
|
if(command.isResponse()){
|
||||||
if( command.isResponse() ) {
|
Response response=(Response)command;
|
||||||
Response response = (Response) command;
|
FutureResponse future=null;
|
||||||
FutureResponse future = (FutureResponse) requestMap.remove(new Integer(response.getCorrelationId()));
|
synchronized(requestMap){
|
||||||
if( future!=null ) {
|
future=(FutureResponse)requestMap.remove(new Integer(response.getCorrelationId()));
|
||||||
future.set(response);
|
|
||||||
} else {
|
|
||||||
if( debug ) log.debug("Received unexpected response for command id: "+response.getCorrelationId());
|
|
||||||
}
|
}
|
||||||
} else {
|
if(future!=null){
|
||||||
|
future.set(response);
|
||||||
|
}else{
|
||||||
|
if(debug)
|
||||||
|
log.debug("Received unexpected response for command id: "+response.getCorrelationId());
|
||||||
|
}
|
||||||
|
}else{
|
||||||
getTransportListener().onCommand(command);
|
getTransportListener().onCommand(command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If an async exception occurs, then assume no responses will arrive for any of
|
* If an async exception occurs, then assume no responses will arrive for any of current requests. Lets let them
|
||||||
* current requests. Lets let them know of the problem.
|
* know of the problem.
|
||||||
*/
|
*/
|
||||||
public void onException(IOException error) {
|
public void onException(IOException error){
|
||||||
|
|
||||||
// Copy and Clear the request Map
|
// Copy and Clear the request Map
|
||||||
ArrayList requests = new ArrayList(requestMap.values());
|
ArrayList requests=new ArrayList(requestMap.values());
|
||||||
requestMap.clear();
|
requestMap.clear();
|
||||||
|
for(Iterator iter=requests.iterator();iter.hasNext();){
|
||||||
for (Iterator iter = requests.iterator(); iter.hasNext();) {
|
FutureResponse fr=(FutureResponse)iter.next();
|
||||||
FutureResponse fr = (FutureResponse) iter.next();
|
|
||||||
fr.set(new ExceptionResponse(error));
|
fr.set(new ExceptionResponse(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
super.onException(error);
|
super.onException(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntSequenceGenerator getSequenceGenerator() {
|
public IntSequenceGenerator getSequenceGenerator(){
|
||||||
return sequenceGenerator;
|
return sequenceGenerator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString(){
|
||||||
return next.toString();
|
return next.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue