alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

ActiveMQ example source code file (KahaTransaction.java)

This example ActiveMQ source code file (KahaTransaction.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - ActiveMQ tags/keywords

arraylist, arraylist, io, ioexception, kahatransaction, list, list, message, messageack, messageack, messageid, messagestore, string, txcommand, txcommand, util

The ActiveMQ KahaTransaction.java source code

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package org.apache.activemq.store.kahadaptor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.command.BaseCommand;
import org.apache.activemq.command.Message;
import org.apache.activemq.command.MessageAck;
import org.apache.activemq.command.MessageId;
import org.apache.activemq.store.MessageStore;
import org.apache.activemq.store.TopicMessageStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Stores a messages/acknowledgements for a transaction
 * 
 * 
 */
class KahaTransaction {
    protected List<TxCommand> list = new ArrayList();

    void add(KahaMessageStore store, BaseCommand command) {
        TxCommand tx = new TxCommand();
        tx.setCommand(command);
        tx.setMessageStoreKey(store.getId());
        list.add(tx);
    }

    public void add(KahaMessageStore destination, String clientId, String subscriptionName, MessageId messageId, MessageAck ack) {
        TxCommand tx = new TxCommand();
        tx.setCommand(ack);
        tx.setMessageStoreKey(destination.getId());
        tx.setClientId(clientId);
        tx.setSubName(subscriptionName);
        tx.setMessageId(messageId);
        list.add(tx);
    }

    Message[] getMessages() {
        List<BaseCommand> result = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            TxCommand command = list.get(i);
            if (command.isAdd()) {
                result.add(command.getCommand());
            }
        }
        Message[] messages = new Message[result.size()];
        return result.toArray(messages);
    }

    MessageAck[] getAcks() {
        List<BaseCommand> result = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            TxCommand command = list.get(i);
            if (command.isRemove()) {
                result.add(command.getCommand());
            }
        }
        MessageAck[] acks = new MessageAck[result.size()];
        return result.toArray(acks);
    }

    void prepare() {
    }

    void rollback() {
        list.clear();
    }

    /**
     * @throws IOException
     */
    void commit(KahaTransactionStore transactionStore) throws IOException {
        for (int i = 0; i < list.size(); i++) {
            TxCommand command = list.get(i);
            MessageStore ms = transactionStore.getStoreById(command.getMessageStoreKey());
            if (command.isAdd()) {
                ms.addMessage(null, (Message)command.getCommand());
            }
        }
        for (int i = 0; i < list.size(); i++) {
            TxCommand command = list.get(i);
            MessageStore ms = transactionStore.getStoreById(command.getMessageStoreKey());
            if (command.isRemove()) {
                ms.removeMessage(null, (MessageAck)command.getCommand());
            } else if (command.isAck()) {
                ((TopicMessageStore)ms).acknowledge(null, command.getClientId(), command.getSubscriptionName(),
                        command.getMessageId(), (MessageAck)command.getCommand());
            }
        }
    }

    List<TxCommand> getList() {
        return new ArrayList<TxCommand>(list);
    }

    void setList(List<TxCommand> list) {
        this.list = list;
    }
}

Other ActiveMQ examples (source code examples)

Here is a short list of links related to this ActiveMQ KahaTransaction.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.