com.gs.reladomo.jms.TopicResourcesWithTransactionXid Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reladomo-xa Show documentation
Show all versions of reladomo-xa Show documentation
Reladomo integration with XA resources, such as JMS
/*
Copyright 2018 Goldman Sachs.
Licensed under the Apache License, Version 2.0 (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
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
// Portions copyright Hiroshi Ito. Licensed under Apache 2.0 license
package com.gs.reladomo.jms;
import com.gs.fw.common.mithra.MithraManagerProvider;
import com.gs.fw.common.mithra.MithraTransaction;
import com.gs.fw.common.mithra.MithraTransactionalList;
import com.gs.fw.common.mithra.TransactionalCommand;
import com.gs.fw.common.mithra.attribute.Attribute;
import com.gs.fw.common.mithra.finder.Operation;
import com.gs.fw.common.mithra.transaction.MultiThreadedTm;
import com.gs.fw.common.mithra.util.serializer.DeserializationClassMetaData;
import com.gs.fw.common.mithra.util.serializer.DeserializationException;
import com.gs.reladomo.txid.ReladomoTxIdInterface;
import com.gs.reladomo.txid.ReladomoTxIdInterfaceFinder;
import com.gs.reladomo.util.Base64;
import com.gs.reladomo.util.InterruptableBackoff;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class TopicResourcesWithTransactionXid
{
public static final int DEFAULT_WAIT_TIME_TO_CONNECT = 9 * 60 * 1000; // default 9 minutes. we use retry with exponential back-off: 2^10 > 9*60 > 2^9
private final Logger logger;
private final long maxWaitTimeToConnect;
private final MultiThreadedTm multiThreadedTm;
private final List jmsTopics = FastList.newList();
private int transactionXidId;
private final String flowId;
private Attribute txIdSourceAttribute;
private Object sourceAttributeValue;
private ReladomoTxIdInterfaceFinder txIdFinder;
public TopicResourcesWithTransactionXid(ReladomoTxIdInterfaceFinder txIdFinder, long maxWaitTimeToConnect, MultiThreadedTm multiThreadedTm, String flowId, Object sourceAttributeValue)
{
this.multiThreadedTm = multiThreadedTm;
this.flowId = flowId;
this.maxWaitTimeToConnect = maxWaitTimeToConnect;
logger = LoggerFactory.getLogger("TopicResourcesWithTransactionXid."+flowId);
this.txIdSourceAttribute = txIdFinder.getSourceAttribute();
this.sourceAttributeValue = sourceAttributeValue;
this.txIdFinder = txIdFinder;
if (txIdSourceAttribute != null)
{
if (sourceAttributeValue == null)
{
throw new IllegalArgumentException("sourceAttributeValue must be specified if the txIdFinder has a source attribute");
}
}
else
{
if (sourceAttributeValue != null)
{
throw new IllegalArgumentException("sourceAttributeValue must be null if the txIdFinder has no source attribute");
}
}
}
public void addJmsTopic(JmsTopic jmsTopic)
{
this.jmsTopics.add(jmsTopic);
}
public OutgoingAsyncTopic connectOutgoingTopicWithRetry(JmsTopicConfig topicConfig, String outgoingThreadNamePostFix, OutgoingTopicListener listener)
{
long start = System.currentTimeMillis();
long retryWait = 1000;
while(true)
{
try
{
OutgoingAsyncTopic outgoingAsyncTopic = new OutgoingAsyncTopic(topicConfig, this.multiThreadedTm, outgoingThreadNamePostFix, listener);
this.addJmsTopic(outgoingAsyncTopic);
return outgoingAsyncTopic;
}
catch (Exception e)
{
retryWait = this.handleTopicRetry(topicConfig, start, retryWait, e);
}
}
}
public IncomingTopic connectIncomingTopicWithRetry(JmsTopicConfig incomingTopicConfig, InterruptableBackoff interruptableBackoff)
{
long start = System.currentTimeMillis();
long retryWait = 1000;
while(true)
{
try
{
IncomingTopic result = new IncomingTopic(incomingTopicConfig, this.multiThreadedTm, interruptableBackoff);
this.addJmsTopic(result);
return result;
}
catch (Exception e)
{
retryWait = this.handleTopicRetry(incomingTopicConfig, start, retryWait, e);
}
}
}
public MithraTransaction startTransaction()
{
MithraTransaction mithraTransaction = MithraManagerProvider.getMithraManager().startOrContinueTransaction();
updateXidRecord();
return mithraTransaction;
}
public void updateXidRecord()
{
final byte[] globalTransactionId = this.multiThreadedTm.getLocalTransaction().getGlobalTransactionId();
MithraManagerProvider.getMithraManager().executeTransactionalCommandInSeparateThread(new TransactionalCommand
© 2015 - 2025 Weber Informatics LLC | Privacy Policy