Skip to content

Commit

Permalink
Add initial version of transaction counting handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Isuru Wijesiri committed Oct 25, 2023
1 parent fd29f3d commit 055227b
Show file tree
Hide file tree
Showing 18 changed files with 1,389 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ cert/
# Ballerina related
target/
Dependencies.toml

# Mac related
.DS_Store

# IDE related
.idea/
.vscode/
158 changes: 158 additions & 0 deletions counter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
# Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
#
# 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.wso2.integration.transaction.counter</groupId>
<artifactId>transacton-count-handler</artifactId>
<packaging>bundle</packaging>
<version>1.0.0</version>

<name>WSO2 Integration Transaction Counting Handler</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<commons-logging.version>1.1.1</commons-logging.version>
<synapse.version>[4.0.0,5.0.0)</synapse.version>
<google.code.gson.version>2.9.1</google.code.gson.version>
<httpcore.version>4.4.16.wso2v1</httpcore.version>
<httpcomponent.version>4.2.5.wso2v1</httpcomponent.version>
</properties>

<repositories>
<repository>
<id>wso2-nexus</id>
<name>WSO2 internal Repository</name>
<url>https://maven.wso2.org/nexus/content/groups/wso2-public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
</repository>

<repository>
<id>wso2.releases</id>
<name>WSO2 internal Repository</name>
<url>https://maven.wso2.org/nexus/content/repositories/releases/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>wso2.releases</id>
<name>WSO2 internal Repository</name>
<url>https://maven.wso2.org/nexus/content/repositories/releases/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
</pluginRepository>

<pluginRepository>
<id>wso2.snapshots</id>
<name>WSO2 Snapshot Repository</name>
<url>https://maven.wso2.org/nexus/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
<pluginRepository>
<id>wso2-nexus</id>
<name>WSO2 internal Repository</name>
<url>https://maven.wso2.org/nexus/content/groups/wso2-public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons-logging.version}</version>
</dependency>
<dependency>
<groupId>org.apache.synapse</groupId>
<artifactId>synapse-core</artifactId>
<version>${synapse.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${google.code.gson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.wso2</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcore.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.wso2</groupId>
<artifactId>httpclient</artifactId>
<version>${httpcomponent.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>5.1.2</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Export-Package>
org.wso2.integration.transaction.counter.*,
</Export-Package>
<Import-Package>
org.apache.synapse.core,
*;resolution:=optional
</Import-Package>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC. 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.wso2.integration.transaction.counter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.AbstractExtendedSynapseHandler;
import org.apache.synapse.MessageContext;
import org.wso2.integration.transaction.counter.consumer.TransactionRecordConsumer;
import org.wso2.integration.transaction.counter.exception.TransactionCounterConfigurationException;
import org.wso2.integration.transaction.counter.producer.TransactionRecordProducer;
import org.wso2.integration.transaction.counter.queue.TransactionRecordQueue;
import org.wso2.integration.transaction.counter.record.TransactionRecord;
import org.wso2.integration.transaction.counter.store.TransactionRecordStore;
import org.wso2.integration.transaction.counter.config.TransactionCounterConfig;

import java.lang.reflect.Constructor;

/**
* This class is the starting point of the transaction counter. This class is responsible for initializing all the
* components of the transaction counter and starting them. This class extends the Synapse handler interface and is
* responsible for intercepting the Synapse message flow and passing the message context to the transaction counting
* logic. This is registered in handlers.xml in case of APIM and service bus initializer in case of MI/ESB.
* @author - Isuru Wijesiri
* @version - 1.0.0
*/
public class TransactionCountHandler extends AbstractExtendedSynapseHandler {
private static final Log LOG = LogFactory.getLog(TransactionCountHandler.class);
private TransactionRecordQueue transactionRecordQueue;
private TransactionRecordProducer transactionRecordProducer;
private TransactionRecordConsumer transactionRecordConsumer;
private TransactionRecordStore transactionCountStore;
private static boolean enabled = false;

public TransactionCountHandler() {

// Initialize the config mechanism
try {
TransactionCounterConfig.init();
} catch (TransactionCounterConfigurationException e) {
LOG.error("Error while initializing Transaction Counter. Transaction counter will be disabled", e);
return;
}

try {
Class<?> clazz = Class.forName(
TransactionCounterConfig.getTransactionCountStoreClass()
);
Constructor<?> constructor = clazz.getConstructor();
this.transactionCountStore = (TransactionRecordStore) constructor.newInstance();
} catch (Exception e) {
LOG.error("Error while initializing Transaction Counter. Transaction counter will be disabled", e);
return;
}

this.transactionRecordProducer = TransactionRecordProducer.getInstance();
this.transactionRecordConsumer = TransactionRecordConsumer.getInstance();
this.transactionRecordQueue = TransactionRecordQueue.getInstance();

TransactionRecord.init(
TransactionCounterConfig.getServerID(),
TransactionCounterConfig.getServerType().toString()
);

this.transactionRecordQueue.init(
TransactionCounterConfig.getTransactionRecordQueueSize()
);

this.transactionRecordProducer.init(
transactionRecordQueue,
TransactionCounterConfig.getProducerThreadPoolSize(),
TransactionCounterConfig.getMaxTransactionCount(),
TransactionCounterConfig.getMinTransactionCount(),
TransactionCounterConfig.getTransactionCountRecordInterval());

this.transactionCountStore.init(
TransactionCounterConfig.getTransactionCountService(),
TransactionCounterConfig.getTransactionCountServiceUsername(),
TransactionCounterConfig.getTransactionCountServicePassword()
);

this.transactionRecordConsumer.init(
transactionCountStore,
transactionRecordQueue,
TransactionCounterConfig.getConsumerCommitInterval(),
TransactionCounterConfig.getMaxRetryCount(),
TransactionCounterConfig.getMaxTransactionRecordsPerCommit());

enabled = true;
}

@Override
public boolean handleRequestInFlow(MessageContext messageContext) {
if(!enabled) {
return true;
}
int tCount = TransactionCountingLogic.handleRequestInFlow(messageContext);
if(tCount > 0) {
this.transactionRecordProducer.addTransaction(tCount);
}
return true;
}

@Override
public boolean handleRequestOutFlow(MessageContext messageContext) {
if(!enabled) {
return true;
}
int tCount = TransactionCountingLogic.handleRequestOutFlow(messageContext);
if(tCount > 0) {
this.transactionRecordProducer.addTransaction(tCount);
}
return true;
}

@Override
public boolean handleResponseInFlow(MessageContext messageContext) {
if(!enabled) {
return true;
}
int tCount = TransactionCountingLogic.handleResponseInFlow(messageContext);
if(tCount > 0) {
this.transactionRecordProducer.addTransaction(tCount);
}
return true;
}

@Override
public boolean handleResponseOutFlow(MessageContext messageContext) {
if(!enabled) {
return true;
}
int tCount = TransactionCountingLogic.handleResponseOutFlow(messageContext);
if(tCount > 0) {
this.transactionRecordProducer.addTransaction(tCount);
}
return true;
}

@Override
public boolean handleServerInit() {
// Nothing to implement
return true;
}

@Override
public boolean handleServerShutDown() {
// Clen up resources
transactionRecordProducer.shutdown();
transactionRecordConsumer.shutdown();
transactionRecordQueue.clenUp();
transactionCountStore.clenUp();
return true;
}

@Override
public boolean handleArtifactDeployment(String s, String s1, String s2) {
// Nothing to implement
return true;
}

@Override
public boolean handleArtifactUnDeployment(String s, String s1, String s2) {
// Nothing to implement
return true;
}

@Override
public boolean handleError(MessageContext messageContext) {
// Nothing to implement
return true;
}
}
Loading

0 comments on commit 055227b

Please sign in to comment.