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

add a keepAlive example #26

Merged
merged 1 commit into from
Jul 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions modules/clients/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,15 @@ tasks.register('runClose', JavaExec) {
systemProperty 'org.slf4j.simpleLogger.showShortLogName', 'true'
systemProperty 'org.slf4j.simpleLogger.levelInBrackets', 'true'
}

tasks.register('runKeepAlive', JavaExec) {
classpath = sourceSets.main.runtimeClasspath
mainClass = 'com.tsurugidb.tsubakuro.examples.keepAlive.Main'

systemProperty 'tsurugi.dbname', findProperty('tsurugi.dbname') ?: 'ipc:tsurugi'

systemProperty 'org.slf4j.simpleLogger.defaultLogLevel', findProperty('loglevel') ?: 'info'
systemProperty 'org.slf4j.simpleLogger.showThreadName', 'false'
systemProperty 'org.slf4j.simpleLogger.showShortLogName', 'true'
systemProperty 'org.slf4j.simpleLogger.levelInBrackets', 'true'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.tsurugidb.tsubakuro.examples.keepAlive;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.tsurugidb.tsubakuro.common.Session;
import com.tsurugidb.tsubakuro.common.SessionBuilder;
import com.tsurugidb.tsubakuro.exception.ServerException;
import com.tsurugidb.tsubakuro.channel.common.connection.UsernamePasswordCredential;

public final class Main {
private Main(String[] args) {
}

// set dbname as follows
// -Ptsurugi.dbname=ipc:tsurugi or
// -Ptsurugi.dbname=tcp://localhost:12345
// when run this example by `./gradlew run` command
private static String url = System.getProperty("tsurugi.dbname");

private static boolean keepAlive = true;
private static long interval = 1000;
private static long loopCount = 200;
private static long timeout = 0;

public static void main(String[] args) {
// コマンドラインオプションの設定
Options options = new Options();

options.addOption(Option.builder("n").argName("noKeepAlive").desc("No keep alive.").build());
options.addOption(Option.builder("l").argName("loopCount").hasArg().desc("Loop count.").build());
options.addOption(Option.builder("t").argName("timeout").hasArg().desc("Timeout.").build());

CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;

try {
cmd = parser.parse(options, args);

if (cmd.hasOption("n")) {
keepAlive = false;
System.err.println("no keep alive");
}
if (cmd.hasOption("l")) {
loopCount = Integer.parseInt(cmd.getOptionValue("l"));
System.err.println("loop count = " + loopCount);
}
if (cmd.hasOption("t")) {
timeout = Integer.parseInt(cmd.getOptionValue("t"));
System.err.println("timeout = " + timeout);
}
} catch (ParseException e) {
System.err.println("cmd parser failed." + e);
}

try (
Session session = SessionBuilder.connect(url)
.withCredential(new UsernamePasswordCredential("user", "pass"))
.withKeepAlive(keepAlive)
.create(10, TimeUnit.SECONDS)) {
if (timeout > 0) {
session.updateExpirationTime(timeout, TimeUnit.SECONDS);
}
int n = 0;
while (true) {
if (!session.isAlive()) {
System.out.println("detect tsurugidb session is not alive.");
break;
}
if (++n > loopCount) {
System.out.println("reach loop count (" + loopCount + ")");
break;
}
System.out.println("tsurugidb session is alive, N = " + n);
Thread.sleep(interval);
}
} catch (IOException | ServerException | InterruptedException | TimeoutException e) {
System.out.println(e);
}
System.out.println("exiting.");
}
}