Merge branch 'master' into pr/991
This commit is contained in:
commit
4989946b09
|
@ -0,0 +1,63 @@
|
|||
<?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>com.baeldung</groupId>
|
||||
<artifactId>JGitSnippets</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jgit-repository</id>
|
||||
<url>https://repo.eclipse.org/content/groups/releases/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<!-- Core Library -->
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jgit</groupId>
|
||||
<artifactId>org.eclipse.jgit</artifactId>
|
||||
<version>4.5.0.201609210915-r</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jgit</groupId>
|
||||
<artifactId>org.eclipse.jgit.archive</artifactId>
|
||||
<version>4.5.0.201609210915-r</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.21</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<configuration>
|
||||
<source>1.7</source>
|
||||
<target>1.7</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.jgit;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
|
||||
/**
|
||||
* Simple snippet which shows how to create a new repository
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class CreateNewRepository {
|
||||
|
||||
public static void main(String[] args) throws IOException, IllegalStateException, GitAPIException {
|
||||
// prepare a new folder
|
||||
File localPath = File.createTempFile("TestGitRepository", "");
|
||||
if(!localPath.delete()) {
|
||||
throw new IOException("Could not delete temporary file " + localPath);
|
||||
}
|
||||
|
||||
// create the directory
|
||||
try (Git git = Git.init().setDirectory(localPath).call()) {
|
||||
System.out.println("Having repository: " + git.getRepository().getDirectory());
|
||||
}
|
||||
|
||||
FileUtils.deleteDirectory(localPath);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.jgit;
|
||||
|
||||
import com.baeldung.jgit.helper.Helper;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Simple snippet which shows how to open an existing repository
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class OpenRepository {
|
||||
|
||||
public static void main(String[] args) throws IOException, GitAPIException {
|
||||
// first create a test-repository, the return is including the .get directory here!
|
||||
File repoDir = createSampleGitRepo();
|
||||
|
||||
// now open the resulting repository with a FileRepositoryBuilder
|
||||
FileRepositoryBuilder builder = new FileRepositoryBuilder();
|
||||
try (Repository repository = builder.setGitDir(repoDir)
|
||||
.readEnvironment() // scan environment GIT_* variables
|
||||
.findGitDir() // scan up the file system tree
|
||||
.build()) {
|
||||
System.out.println("Having repository: " + repository.getDirectory());
|
||||
|
||||
// the Ref holds an ObjectId for any type of object (tree, commit, blob, tree)
|
||||
Ref head = repository.exactRef("refs/heads/master");
|
||||
System.out.println("Ref of refs/heads/master: " + head);
|
||||
}
|
||||
}
|
||||
|
||||
private static File createSampleGitRepo() throws IOException, GitAPIException {
|
||||
try (Repository repository = Helper.createNewRepository()) {
|
||||
System.out.println("Temporary repository at " + repository.getDirectory());
|
||||
|
||||
// create the file
|
||||
File myfile = new File(repository.getDirectory().getParent(), "testfile");
|
||||
if(!myfile.createNewFile()) {
|
||||
throw new IOException("Could not create file " + myfile);
|
||||
}
|
||||
|
||||
// run the add-call
|
||||
try (Git git = new Git(repository)) {
|
||||
git.add()
|
||||
.addFilepattern("testfile")
|
||||
.call();
|
||||
|
||||
|
||||
// and then commit the changes
|
||||
git.commit()
|
||||
.setMessage("Added testfile")
|
||||
.call();
|
||||
}
|
||||
|
||||
System.out.println("Added file " + myfile + " to repository at " + repository.getDirectory());
|
||||
|
||||
return repository.getDirectory();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
|
||||
package com.baeldung.jgit.helper;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
||||
|
||||
public class Helper {
|
||||
|
||||
public static Repository openJGitRepository() throws IOException {
|
||||
FileRepositoryBuilder builder = new FileRepositoryBuilder();
|
||||
return builder
|
||||
.readEnvironment() // scan environment GIT_* variables
|
||||
.findGitDir() // scan up the file system tree
|
||||
.build();
|
||||
}
|
||||
|
||||
public static Repository createNewRepository() throws IOException {
|
||||
// prepare a new folder
|
||||
File localPath = File.createTempFile("TestGitRepository", "");
|
||||
if(!localPath.delete()) {
|
||||
throw new IOException("Could not delete temporary file " + localPath);
|
||||
}
|
||||
|
||||
// create the directory
|
||||
Repository repository = FileRepositoryBuilder.create(new File(localPath, ".git"));
|
||||
repository.create();
|
||||
|
||||
return repository;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.jgit.porcelain;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import com.baeldung.jgit.helper.Helper;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
|
||||
/**
|
||||
* Simple snippet which shows how to add a file to the index
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class AddFile {
|
||||
|
||||
public static void main(String[] args) throws IOException, GitAPIException {
|
||||
// prepare a new test-repository
|
||||
try (Repository repository = Helper.createNewRepository()) {
|
||||
try (Git git = new Git(repository)) {
|
||||
// create the file
|
||||
File myfile = new File(repository.getDirectory().getParent(), "testfile");
|
||||
if(!myfile.createNewFile()) {
|
||||
throw new IOException("Could not create file " + myfile);
|
||||
}
|
||||
|
||||
// run the add-call
|
||||
git.add()
|
||||
.addFilepattern("testfile")
|
||||
.call();
|
||||
|
||||
System.out.println("Added file " + myfile + " to repository at " + repository.getDirectory());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.jgit.porcelain;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import com.baeldung.jgit.helper.Helper;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
|
||||
/**
|
||||
* Simple snippet which shows how to commit all files
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class CommitAll {
|
||||
|
||||
public static void main(String[] args) throws IOException, GitAPIException {
|
||||
// prepare a new test-repository
|
||||
try (Repository repository = Helper.createNewRepository()) {
|
||||
try (Git git = new Git(repository)) {
|
||||
// create the file
|
||||
File myfile = new File(repository.getDirectory().getParent(), "testfile");
|
||||
if(!myfile.createNewFile()) {
|
||||
throw new IOException("Could not create file " + myfile);
|
||||
}
|
||||
|
||||
// Stage all files in the repo including new files
|
||||
git.add().addFilepattern(".").call();
|
||||
|
||||
// and then commit the changes.
|
||||
git.commit()
|
||||
.setMessage("Commit all changes including additions")
|
||||
.call();
|
||||
|
||||
try(PrintWriter writer = new PrintWriter(myfile)) {
|
||||
writer.append("Hello, world!");
|
||||
}
|
||||
|
||||
// Stage all changed files, omitting new files, and commit with one command
|
||||
git.commit()
|
||||
.setAll(true)
|
||||
.setMessage("Commit changes to all files")
|
||||
.call();
|
||||
|
||||
|
||||
System.out.println("Committed all changes to repository at " + repository.getDirectory());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.jgit.porcelain;
|
||||
|
||||
import java.io.IOException;
|
||||
import com.baeldung.jgit.helper.Helper;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
|
||||
/**
|
||||
* Simple snippet which shows how to create a tag
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class CreateAndDeleteTag {
|
||||
|
||||
public static void main(String[] args) throws IOException, GitAPIException {
|
||||
// prepare test-repository
|
||||
try (Repository repository = Helper.openJGitRepository()) {
|
||||
try (Git git = new Git(repository)) {
|
||||
// remove the tag before creating it
|
||||
git.tagDelete().setTags("tag_for_testing").call();
|
||||
|
||||
// set it on the current HEAD
|
||||
Ref tag = git.tag().setName("tag_for_testing").call();
|
||||
System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());
|
||||
|
||||
// remove the tag again
|
||||
git.tagDelete().setTags("tag_for_testing").call();
|
||||
|
||||
// read some other commit and set the tag on it
|
||||
ObjectId id = repository.resolve("HEAD^");
|
||||
try (RevWalk walk = new RevWalk(repository)) {
|
||||
RevCommit commit = walk.parseCommit(id);
|
||||
tag = git.tag().setObjectId(commit).setName("tag_for_testing").call();
|
||||
System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());
|
||||
|
||||
// remove the tag again
|
||||
git.tagDelete().setTags("tag_for_testing").call();
|
||||
|
||||
// create an annotated tag
|
||||
tag = git.tag().setName("tag_for_testing").setAnnotated(true).call();
|
||||
System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());
|
||||
|
||||
// remove the tag again
|
||||
git.tagDelete().setTags("tag_for_testing").call();
|
||||
|
||||
walk.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package com.baeldung.jgit.porcelain;
|
||||
|
||||
import java.io.IOException;
|
||||
import com.baeldung.jgit.helper.Helper;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
|
||||
/**
|
||||
* Simple snippet which shows how to get the commit-ids for a file to provide log information.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class Log {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static void main(String[] args) throws IOException, GitAPIException {
|
||||
try (Repository repository = Helper.openJGitRepository()) {
|
||||
try (Git git = new Git(repository)) {
|
||||
Iterable<RevCommit> logs = git.log()
|
||||
.call();
|
||||
int count = 0;
|
||||
for (RevCommit rev : logs) {
|
||||
//System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
|
||||
count++;
|
||||
}
|
||||
System.out.println("Had " + count + " commits overall on current branch");
|
||||
|
||||
logs = git.log()
|
||||
.add(repository.resolve("remotes/origin/testbranch"))
|
||||
.call();
|
||||
count = 0;
|
||||
for (RevCommit rev : logs) {
|
||||
System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
|
||||
count++;
|
||||
}
|
||||
System.out.println("Had " + count + " commits overall on test-branch");
|
||||
|
||||
logs = git.log()
|
||||
.all()
|
||||
.call();
|
||||
count = 0;
|
||||
for (RevCommit rev : logs) {
|
||||
//System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
|
||||
count++;
|
||||
}
|
||||
System.out.println("Had " + count + " commits overall in repository");
|
||||
|
||||
logs = git.log()
|
||||
// for all log.all()
|
||||
.addPath("README.md")
|
||||
.call();
|
||||
count = 0;
|
||||
for (RevCommit rev : logs) {
|
||||
//System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
|
||||
count++;
|
||||
}
|
||||
System.out.println("Had " + count + " commits on README.md");
|
||||
|
||||
logs = git.log()
|
||||
// for all log.all()
|
||||
.addPath("pom.xml")
|
||||
.call();
|
||||
count = 0;
|
||||
for (RevCommit rev : logs) {
|
||||
//System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
|
||||
count++;
|
||||
}
|
||||
System.out.println("Had " + count + " commits on pom.xml");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
import com.baeldung.jgit.helper.Helper;
|
||||
import org.eclipse.jgit.lib.ObjectLoader;
|
||||
import org.eclipse.jgit.lib.ObjectReader;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.junit.Test;
|
||||
import java.io.IOException;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* Tests which show issues with JGit that we reported upstream.
|
||||
*/
|
||||
public class JGitBugTest {
|
||||
@Test
|
||||
public void testRevWalkDisposeClosesReader() throws IOException {
|
||||
try (Repository repo = Helper.openJGitRepository()) {
|
||||
try (ObjectReader reader = repo.newObjectReader()) {
|
||||
try (RevWalk walk = new RevWalk(reader)) {
|
||||
walk.dispose();
|
||||
|
||||
Ref head = repo.exactRef("refs/heads/master");
|
||||
System.out.println("Found head: " + head);
|
||||
|
||||
ObjectLoader loader = reader.open(head.getObjectId());
|
||||
assertNotNull(loader);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.jgit.porcelain;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PorcelainTest {
|
||||
@Test
|
||||
public void runSamples() throws Exception {
|
||||
// simply call all the samples to see any severe problems with the samples
|
||||
AddFile.main(null);
|
||||
|
||||
CommitAll.main(null);
|
||||
|
||||
CreateAndDeleteTag.main(null);
|
||||
|
||||
Log.main(null);
|
||||
}
|
||||
}
|
|
@ -38,6 +38,11 @@
|
|||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.retry</groupId>
|
||||
<artifactId>spring-retry</artifactId>
|
||||
<version>${springretry.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- aspectj -->
|
||||
|
||||
|
@ -166,7 +171,6 @@
|
|||
<artifactId>ehcache</artifactId>
|
||||
<version>${ehcache.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
|
@ -275,6 +279,7 @@
|
|||
<!-- Spring -->
|
||||
<org.springframework.version>4.3.4.RELEASE</org.springframework.version>
|
||||
<org.springframework.security.version>4.2.0.RELEASE</org.springframework.security.version>
|
||||
<springretry.version>1.1.5.RELEASE</springretry.version>
|
||||
|
||||
<!-- persistence -->
|
||||
<hibernate.version>5.2.5.Final</hibernate.version>
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package org.baeldung.springretry;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.retry.annotation.EnableRetry;
|
||||
import org.springframework.retry.backoff.FixedBackOffPolicy;
|
||||
import org.springframework.retry.policy.SimpleRetryPolicy;
|
||||
import org.springframework.retry.support.RetryTemplate;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "org.baeldung.springretry")
|
||||
@EnableRetry
|
||||
//Uncomment this two lines if we need XML configuration
|
||||
//@EnableAspectJAutoProxy
|
||||
//@ImportResource("classpath:/retryadvice.xml")
|
||||
public class AppConfig {
|
||||
|
||||
@Bean
|
||||
public RetryTemplate retryTemplate() {
|
||||
RetryTemplate retryTemplate = new RetryTemplate();
|
||||
|
||||
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
|
||||
fixedBackOffPolicy.setBackOffPeriod(2000l);
|
||||
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
|
||||
|
||||
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
|
||||
retryPolicy.setMaxAttempts(2);
|
||||
retryTemplate.setRetryPolicy(retryPolicy);
|
||||
|
||||
retryTemplate.registerListener(new DefaultListenerSupport());
|
||||
return retryTemplate;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package org.baeldung.springretry;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.retry.RetryCallback;
|
||||
import org.springframework.retry.RetryContext;
|
||||
import org.springframework.retry.listener.RetryListenerSupport;
|
||||
|
||||
public class DefaultListenerSupport extends RetryListenerSupport {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DefaultListenerSupport.class);
|
||||
|
||||
@Override
|
||||
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
|
||||
logger.info("onClose");
|
||||
super.close(context, callback, throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
|
||||
logger.info("onError");
|
||||
super.onError(context, callback, throwable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
|
||||
logger.info("onOpen");
|
||||
return super.open(context, callback);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package org.baeldung.springretry;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.retry.annotation.Backoff;
|
||||
import org.springframework.retry.annotation.Recover;
|
||||
import org.springframework.retry.annotation.Retryable;
|
||||
|
||||
public interface MyService {
|
||||
|
||||
@Retryable
|
||||
void retryService();
|
||||
|
||||
@Retryable(value = { SQLException.class }, maxAttempts = 2, backoff = @Backoff(delay = 5000))
|
||||
void retryServiceWithRecovery(String sql) throws SQLException;
|
||||
|
||||
@Recover
|
||||
void recover(SQLException e, String sql);
|
||||
|
||||
void templateRetryService();
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package org.baeldung.springretry;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@Service
|
||||
public class MyServiceImpl implements MyService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MyServiceImpl.class);
|
||||
|
||||
@Override
|
||||
public void retryService() {
|
||||
logger.info("throw RuntimeException in method retryService()");
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void retryServiceWithRecovery(String sql) throws SQLException {
|
||||
if (StringUtils.isEmpty(sql)) {
|
||||
logger.info("throw SQLException in method retryServiceWithRecovery()");
|
||||
throw new SQLException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recover(SQLException e, String sql) {
|
||||
logger.info("In recover method");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void templateRetryService() {
|
||||
logger.info("throw RuntimeException in method templateRetryService()");
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/aop
|
||||
http://www.springframework.org/schema/aop/spring-aop.xsd
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
<aop:config>
|
||||
<aop:pointcut id="transactional"
|
||||
expression="execution(* com.baeldung.springretry..*MyService.defaultXmlRetryService(..))" />
|
||||
<aop:advisor pointcut-ref="transactional" advice-ref="taskRetryAdvice" order="-1" />
|
||||
</aop:config>
|
||||
|
||||
<bean id="taskRetryAdvice" class="org.springframework.retry.interceptor.RetryOperationsInterceptor">
|
||||
<property name="RetryOperations" ref="taskBatchRetryTemplate" />
|
||||
</bean>
|
||||
|
||||
<bean id="taskBatchRetryTemplate" class="org.springframework.retry.support.RetryTemplate">
|
||||
<property name="retryPolicy" ref="taskBatchRetryPolicy" />
|
||||
<property name="backOffPolicy" ref="ExponentialBackOffPolicy" />
|
||||
</bean>
|
||||
|
||||
<bean id="taskBatchRetryPolicy" class="org.springframework.retry.policy.SimpleRetryPolicy">
|
||||
<constructor-arg index="0" value="2" />
|
||||
<constructor-arg index="1">
|
||||
<map>
|
||||
<entry key="java.lang.RuntimeException" value="true" />
|
||||
</map>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="ExponentialBackOffPolicy" class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
|
||||
<property name="initialInterval" value="300">
|
||||
<description>
|
||||
Initial sleep interval value, default 300 ms
|
||||
</description>
|
||||
</property>
|
||||
<property name="maxInterval" value="30000">
|
||||
<description>
|
||||
The maximum value of the backoff period in milliseconds.
|
||||
</description>
|
||||
</property>
|
||||
<property name="multiplier" value="2.0">
|
||||
<description>
|
||||
The value to increment the exp seed with for each retry attempt.
|
||||
</description>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
|
@ -0,0 +1,40 @@
|
|||
package org.baeldung.springretry;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.retry.support.RetryTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
|
||||
public class SpringRetryTest {
|
||||
|
||||
@Autowired
|
||||
private MyService myService;
|
||||
|
||||
@Autowired
|
||||
private RetryTemplate retryTemplate;
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void givenRetryService_whenCallWithException_thenRetry() {
|
||||
myService.retryService();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRetryServiceWithRecovery_whenCallWithException_thenRetryRecover() throws SQLException {
|
||||
myService.retryServiceWithRecovery(null);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void givenTemplateRetryService_whenCallWithException_thenRetry() {
|
||||
retryTemplate.execute(arg0 -> {
|
||||
myService.templateRetryService();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue