ConfigurationContext configurationContext = msgContext.getConfigurationContext();
Options options = msgContext.getOptions();
StorageManager storageManager = SandeshaUtil
.getSandeshaStorageManager(configurationContext);
SequencePropertyBeanMgr seqPropMgr = storageManager.getSequencePropretyBeanMgr();
String toAddress = rmMsgCtx.getTo().getAddress();
String sequenceKey = (String) options.getProperty(SandeshaClientConstants.SEQUENCE_KEY);
String internalSeqenceID = SandeshaUtil.getInternalSequenceID(toAddress,sequenceKey);
String outSequenceID = SandeshaUtil.getSequenceProperty(internalSeqenceID,Sandesha2Constants.SequenceProperties.OUT_SEQUENCE_ID,configurationContext);
if (outSequenceID==null)
throw new SandeshaException ("SequenceID was not found. Cannot send the terminate message");
Transaction addTerminateSeqTransaction = storageManager.getTransaction();
String terminated = SandeshaUtil.getSequenceProperty(outSequenceID,
Sandesha2Constants.SequenceProperties.TERMINATE_ADDED,configurationContext);
//registring an InOutOperationContext for this.
//since the serviceContext.fireAndForget only sets a inOnly One
//this does not work when there is a terminateSequnceResponse
//TODO do processing of terminateMessagesCorrectly., create a new message instead of sendign the one given by the serviceClient
//TODO important
try {
AxisOperation oldOPeration = msgContext.getAxisOperation();
AxisOperation outInAxisOp = new OutInAxisOperation (new QName ("temp"));
//setting flows
outInAxisOp.setRemainingPhasesInFlow(oldOPeration.getRemainingPhasesInFlow());
OperationContext opcontext = OperationContextFactory.createOperationContext(OperationContextFactory.MEP_CONSTANT_OUT_IN,outInAxisOp);
opcontext.setParent(msgContext.getServiceContext());
configurationContext.registerOperationContext(rmMsgCtx.getMessageId(),opcontext);
} catch (AxisFault e1) {
throw new SandeshaException ("Could not register an outInAxisOperation");
}
if (terminated != null
&& "true".equals(terminated)) {
String message = "Terminate was added previously.";
log.info(message);
return;
}
TerminateSequence terminateSequencePart = (TerminateSequence) rmMsgCtx.getMessagePart(Sandesha2Constants.MessageParts.TERMINATE_SEQ);
terminateSequencePart.getIdentifier().setIndentifer(outSequenceID);
rmMsgCtx.setFlow(MessageContext.OUT_FLOW);
msgContext.setProperty(Sandesha2Constants.APPLICATION_PROCESSING_DONE,"true");
rmMsgCtx.setTo(new EndpointReference(toAddress));
String rmVersion = SandeshaUtil.getRMVersion(internalSeqenceID,configurationContext);
if (rmVersion==null)
throw new SandeshaException ("Cant find the rmVersion of the given message");
rmMsgCtx.setWSAAction(SpecSpecificConstants.getTerminateSequenceAction(rmVersion));
rmMsgCtx.setSOAPAction(SpecSpecificConstants.getTerminateSequenceSOAPAction(rmVersion));
String transportTo = SandeshaUtil.getSequenceProperty(internalSeqenceID,Sandesha2Constants.SequenceProperties.TRANSPORT_TO,configurationContext);
if (transportTo!=null) {
rmMsgCtx.setProperty(MessageContextConstants.TRANSPORT_URL,transportTo);
}
try {
rmMsgCtx.addSOAPEnvelope();
} catch (AxisFault e) {
throw new SandeshaException(e.getMessage());
}
String key = SandeshaUtil.getUUID();
SenderBean terminateBean = new SenderBean();
terminateBean.setMessageContextRefKey(key);
storageManager.storeMessageContext(key,msgContext);
//Set a retransmitter lastSentTime so that terminate will be send with
// some delay.
//Otherwise this get send before return of the current request (ack).
//TODO: refine the terminate delay.
terminateBean.setTimeToSend(System.currentTimeMillis()
+ Sandesha2Constants.TERMINATE_DELAY);
terminateBean.setMessageID(msgContext.getMessageID());
//this will be set to true at the sender.
terminateBean.setSend(true);
msgContext.setProperty(Sandesha2Constants.QUALIFIED_FOR_SENDING,
Sandesha2Constants.VALUE_FALSE);
terminateBean.setReSend(false);
SenderBeanMgr retramsmitterMgr = storageManager
.getRetransmitterBeanMgr();
retramsmitterMgr.insert(terminateBean);
SequencePropertyBean terminateAdded = new SequencePropertyBean();
terminateAdded.setName(Sandesha2Constants.SequenceProperties.TERMINATE_ADDED);
terminateAdded.setSequenceID(outSequenceID);
terminateAdded.setValue("true");
seqPropMgr.insert(terminateAdded);
//This should be dumped to the storage by the sender
TransportOutDescription transportOut = msgContext.getTransportOut();
rmMsgCtx.setProperty(Sandesha2Constants.ORIGINAL_TRANSPORT_OUT_DESC,transportOut);
rmMsgCtx.setProperty(Sandesha2Constants.MESSAGE_STORE_KEY,key);