Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JBTM-3005 Update to add a quickstart with the commmon-dbcp2 and tomcat #221

Merged
merged 1 commit into from Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions dbcp2-and-tomcat/README.md
@@ -0,0 +1,33 @@
### Description

This quickstart shows how to get started with Narayana and common-dbcp2 with a simple JDBC example.

### Start Tomcat

Start Tomcat in the usual manner, for example:
`$TOMCAT_HOME/bin/catalina.sh run`

### Build the app

`mvn clean package`

### Deploy the app

`cp target/*.war apache-tomcat-7.0.78/webapps/`

### Get strings from the database

`curl http://localhost:8080/dbcp2-and-tomcat`

### Save string to the database

`curl --data "test" http://localhost:8080/dbcp2-and-tomcat`

### Crash and Recovery

`curl --data "crash" http://localhost:8080/dbcp2-and-tomcat/crash`

Restart Tomcat
`$TOMCAT_HOME/bin/catalina.sh run`
`curl http://localhost:8080/dbcp2-and-tomcat/recovery`

95 changes: 95 additions & 0 deletions dbcp2-and-tomcat/pom.xml
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>io.narayana</groupId>
<artifactId>dbcp2-and-tomcat</artifactId>
<version>5.8.1.Final-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
<version.narayana>5.8.1.Final-SNAPSHOT</version.narayana>
<version.resteasy>3.0.23.Final</version.resteasy>
<version.h2>1.4.195</version.h2>
<version.tomcat.plugin>2.2</version.tomcat.plugin>
<version.commons-dbcp2>2.1.1</version.commons-dbcp2>
<version.commons-pool2>2.4.3</version.commons-pool2>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.jboss.narayana.tomcat</groupId>
<artifactId>tomcat-jta</artifactId>
<version>${version.narayana}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>${version.resteasy}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${version.resteasy}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>${version.commons-dbcp2}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>${version.commons-pool2}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${version.h2}</version>
</dependency>
</dependencies>

<build>
<finalName>${artifactId}</finalName>
</build>

<profiles>
<profile>
<id>unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<build>
<plugins>
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<id>Run tests</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>bash</executable>
<commandlineArgs>${basedir}/run.sh</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
45 changes: 45 additions & 0 deletions dbcp2-and-tomcat/run.sh
@@ -0,0 +1,45 @@
#/bin/bash
set -m

export QUICKSTART_NAME=${PWD##*/}
TOMCAT_VERSION=7.0.82
wget -nc https://archive.apache.org/dist/tomcat/tomcat-7/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.zip
rm -rf apache-tomcat-$TOMCAT_VERSION
unzip apache-tomcat-$TOMCAT_VERSION.zip
export TOMCAT_HOME=$(pwd)/apache-tomcat-$TOMCAT_VERSION/
chmod +x $TOMCAT_HOME/bin/catalina.sh
mvn package
rm -rf $TOMCAT_HOME/webapps/${QUICKSTART_NAME}/
cp target/${QUICKSTART_NAME}.war $TOMCAT_HOME/webapps/
JPDA_SUSPEND=n $TOMCAT_HOME/bin/catalina.sh jpda run &
sleep 10

for i in {1..10}
do
curl -f --data "test$i" http://localhost:8080/${QUICKSTART_NAME}
done
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add in something that calls the crash endpoint and then the recover one and verifies the json has the new string dao please?

Copy link
Contributor Author

@zhfeng zhfeng Apr 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I will do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


curl -f http://localhost:8080/${QUICKSTART_NAME}

# remove all strings
curl -f -X DELETE http://localhost:8080/${QUICKSTART_NAME}

# crash the application
curl -f --data "crash" http://localhost:8080/${QUICKSTART_NAME}/crash

# restart the Tomcat
JPDA_SUSPEND=n $TOMCAT_HOME/bin/catalina.sh jpda run &

# verify the recovery
sleep 5
x=`curl -s http://localhost:8080/${QUICKSTART_NAME}/recovery`

$TOMCAT_HOME/bin/catalina.sh stop
rm -rf apache-tomcat-$TOMCAT_VERSION

if [ "$x" != "[\"crash\"]" ]; then
echo "Crash and Recovery failed"
exit -1
fi

echo "All tests succeeded"
204 changes: 204 additions & 0 deletions dbcp2-and-tomcat/src/main/java/io/narayana/DummyXAResource.java
@@ -0,0 +1,204 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2016, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* 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.
*/
package io.narayana;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

import com.arjuna.ats.arjuna.common.Uid;

/**
* This class is used solely for simulating system crash.
*
* @author <a href="mailto:gytis@redhat.com">Gytis Trikleris</a>
* @author <a href="mailto:zfeng@redhat.com">Amos Feng</a>
*/
public class DummyXAResource implements XAResource {

public static final String LOG_DIR = "target/DummyXAResource/";

private final boolean shouldCrash;

private Xid xid;

private File file;

public DummyXAResource(boolean shouldCrash) {
this.shouldCrash = shouldCrash;
}

/**
* Constructor used by recovery manager to recreate XAResource
*
* @param file File where Xid of the XAResource is stored
*/
public DummyXAResource(File file) throws IOException {
this.shouldCrash = false;
this.file = file;
this.xid = getXidFromFile(file);
}

public int prepare(final Xid xid) throws XAException {
System.out.println("Preparing " + DummyXAResource.class.getSimpleName());

this.file = writeXidToFile(xid, LOG_DIR);

return XA_OK;
}

public void commit(final Xid xid, final boolean arg1) throws XAException {
System.out.println("Committing " + DummyXAResource.class.getSimpleName());

if (shouldCrash) {
System.out.println("Crashing the system");
Runtime.getRuntime().halt(1);
}

removeFile(file);
this.file = null;
this.xid = null;
}

public void rollback(final Xid xid) throws XAException {
System.out.println("Rolling back " + DummyXAResource.class.getSimpleName());

removeFile(file);
this.file = null;
this.xid = null;
}

public boolean isSameRM(XAResource xaResource) throws XAException {
if (!(xaResource instanceof DummyXAResource)) {
return false;
}

DummyXAResource other = (DummyXAResource) xaResource;

return xid != null && other.xid != null && xid.getFormatId() == other.xid.getFormatId()
&& Arrays.equals(xid.getGlobalTransactionId(), other.xid.getGlobalTransactionId())
&& Arrays.equals(xid.getBranchQualifier(), other.xid.getBranchQualifier());
}

public Xid[] recover(int flag) throws XAException {
return new Xid[]{ xid };
}

public void start(Xid xid, int flags) throws XAException {

}

public void end(Xid xid, int flags) throws XAException {

}

public void forget(Xid xid) throws XAException {

}

public int getTransactionTimeout() throws XAException {
return 0;
}

public boolean setTransactionTimeout(final int seconds) throws XAException {
return true;
}

private Xid getXidFromFile(File file) throws IOException {
try (DataInputStream inputStream = new DataInputStream(new FileInputStream(file))) {
int formatId = inputStream.readInt();
int globalTransactionIdLength = inputStream.readInt();
byte[] globalTransactionId = new byte[globalTransactionIdLength];
inputStream.read(globalTransactionId, 0, globalTransactionIdLength);
int branchQualifierLength = inputStream.readInt();
byte[] branchQualifier = new byte[branchQualifierLength];
inputStream.read(branchQualifier, 0, branchQualifierLength);

return new XidImpl(formatId, globalTransactionId, branchQualifier);
}
}

private File writeXidToFile(Xid xid, String directory) throws XAException {
File dir = new File(directory);

if (!dir.mkdirs()) {
throw new XAException(XAException.XAER_RMERR);
}

File file = new File(dir, new Uid().fileStringForm() + "_");

try (DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(file))) {
outputStream.writeInt(xid.getFormatId());
outputStream.writeInt(xid.getGlobalTransactionId().length);
outputStream.write(xid.getGlobalTransactionId(), 0, xid.getGlobalTransactionId().length);
outputStream.writeInt(xid.getBranchQualifier().length);
outputStream.write(xid.getBranchQualifier(), 0, xid.getBranchQualifier().length);
outputStream.flush();
} catch (IOException e) {
throw new XAException(XAException.XAER_RMERR);
}

return file;
}

private void removeFile(File file) throws XAException {
if (file != null) {
if (!file.delete()) {
throw new XAException(XAException.XA_RETRY);
}
}
}

private class XidImpl implements Xid {

private final int formatId;

private final byte[] globalTransactionId;

private final byte[] branchQualifier;

public XidImpl(int formatId, byte[] globalTransactionId, byte[] branchQualifier) {
this.formatId = formatId;
this.globalTransactionId = globalTransactionId;
this.branchQualifier = branchQualifier;
}

@Override
public int getFormatId() {
return formatId;
}

@Override
public byte[] getGlobalTransactionId() {
return globalTransactionId;
}

@Override
public byte[] getBranchQualifier() {
return branchQualifier;
}

}
}