All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.firebus.CorrelationEntry Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
package io.firebus;

import io.firebus.interfaces.CorrelationListener;

public class CorrelationEntry {
	protected int sequence;
	protected Message outboundMessage;
	protected MessageQueue inboundMessages;
	protected CorrelationListener correlationListener;
	protected long timeout;
	protected long expiry;
	
	public CorrelationEntry(long to)
	{
		timeout = to;
		expiry = System.currentTimeMillis() + to;
		sequence = 0;
		inboundMessages = new MessageQueue(100);
	}
	
	public void setListener(CorrelationListener cl, long to)
	{
		timeout = to;
		expiry = System.currentTimeMillis() + timeout;
		correlationListener = cl;
		drainInboundQueue();
	}
	
	public Message popNext() 
	{
		Message next = null;
		int len = inboundMessages.getMessageCount();
		for(int i = 0; i < len; i++) {
			Message msg = inboundMessages.pop();
			if(msg.getCorrelationSequence() == sequence) {
				next = msg;
				sequence++;
				break;
			} else {
				inboundMessages.push(msg);
			}
		}
		return next;
	}

	public void push(Message msg) {
		inboundMessages.push(msg);
		expiry = System.currentTimeMillis() + timeout;
		drainInboundQueue();
	}
	
	
	public void drainInboundQueue() 
	{
		if(correlationListener != null) {
			Message next = null;
			while((next = popNext()) != null) {
				correlationListener.correlatedResponseReceived(outboundMessage, next);
			}
		}
	}
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy