PageRenderTime 43ms CodeModel.GetById 23ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/remote/protocol/versionone/XidTransactionManagementTask.java

#
Java | 98 lines | 55 code | 14 blank | 29 comment | 0 complexity | 0f2d688f95671b087f5d9f63e6800e72 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
 1/*
 2 * JBoss, Home of Professional Open Source.
 3 * Copyright 2011, Red Hat, Inc., and individual contributors
 4 * as indicated by the @author tags. See the copyright.txt file in the
 5 * distribution for a full listing of individual contributors.
 6 *
 7 * This is free software; you can redistribute it and/or modify it
 8 * under the terms of the GNU Lesser General Public License as
 9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22
23package org.jboss.as.ejb3.remote.protocol.versionone;
24
25import javax.transaction.Transaction;
26import javax.transaction.TransactionManager;
27import java.io.IOException;
28
29import org.jboss.as.ejb3.remote.EJBRemoteTransactionsRepository;
30import org.jboss.ejb.client.XidTransactionID;
31import org.jboss.logging.Logger;
32import org.jboss.marshalling.MarshallerFactory;
33import org.jboss.remoting3.Channel;
34import org.xnio.IoUtils;
35
36
37/**
38 * @author Jaikiran Pai
39 */
40abstract class XidTransactionManagementTask implements Runnable {
41
42    private static final Logger logger = Logger.getLogger(XidTransactionManagementTask.class);
43
44    protected final short invocationId;
45    protected final Channel channel;
46    protected final EJBRemoteTransactionsRepository transactionsRepository;
47    protected final XidTransactionID xidTransactionID;
48    protected final MarshallerFactory marshallerFactory;
49    protected final TransactionRequestHandler transactionRequestHandler;
50
51    XidTransactionManagementTask(final TransactionRequestHandler txRequestHandler, final EJBRemoteTransactionsRepository transactionsRepository,
52                                 final MarshallerFactory marshallerFactory, final XidTransactionID xidTransactionID,
53                                 final Channel channel, final short invocationId) {
54
55        this.transactionRequestHandler = txRequestHandler;
56        this.channel = channel;
57        this.marshallerFactory = marshallerFactory;
58        this.invocationId = invocationId;
59        this.transactionsRepository = transactionsRepository;
60        this.xidTransactionID = xidTransactionID;
61    }
62
63    @Override
64    public void run() {
65        try {
66            this.manageTransaction();
67        } catch (Throwable t) {
68            try {
69                logger.error("Error during transaction management of transaction id " + this.xidTransactionID, t);
70                // write out a failure message to the channel to let the client know that
71                // the transaction operation failed
72                transactionRequestHandler.writeException(this.channel, this.marshallerFactory, this.invocationId, t, null);
73            } catch (IOException e) {
74                logger.error("Could not write out message to channel due to", e);
75                // close the channel
76                IoUtils.safeClose(this.channel);
77            }
78            return;
79        }
80
81        try {
82            // write out invocation success message to the channel
83            transactionRequestHandler.writeTxInvocationResponseMessage(this.channel, this.invocationId);
84        } catch (IOException e) {
85            logger.error("Could not write out invocation success message to channel due to", e);
86            // close the channel
87            IoUtils.safeClose(this.channel);
88        }
89    }
90
91    protected abstract void manageTransaction() throws Throwable;
92
93    protected void resumeTransaction(final Transaction transaction) throws Exception {
94        final TransactionManager transactionManager = this.transactionsRepository.getTransactionManager();
95        transactionManager.resume(transaction);
96    }
97
98}