Skip to content

Commit

Permalink
Add a jap store configurer API to allow developer to customize the co…
Browse files Browse the repository at this point in the history
…mmit wait timeout
  • Loading branch information
Paul Warren committed Mar 16, 2018
1 parent 8e189b3 commit d00fb61
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 22 deletions.
Expand Up @@ -3,6 +3,8 @@
import internal.org.springframework.content.jpa.io.DelegatingBlobResourceLoader;
import internal.org.springframework.content.jpa.io.GenericBlobResourceLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.content.jpa.config.JpaStoreProperties;
import org.springframework.content.jpa.config.JpaStoreConfigurer;
import org.springframework.content.jpa.io.BlobResourceLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -18,15 +20,8 @@ public class JpaStoreConfiguration {
@Autowired
private DataSource dataSource;

// @Bean
// public JpaStoreSchemaManager jpaStoreSchemaManager(DataSource ds) {
// return new JpaStoreSchemaManager(ds);
// }
//
// @PostConstruct
// public void schemaSetup() {
// jpaStoreSchemaManager(dataSource).create();
// }
@Autowired(required=false)
private List<JpaStoreConfigurer> configurers;

@Bean
public DelegatingBlobResourceLoader blobResourceLoader(DataSource ds, List<BlobResourceLoader> loaders) {
Expand All @@ -38,13 +33,14 @@ public BlobResourceLoader genericBlobResourceLoader(DataSource ds, PlatformTrans
return new GenericBlobResourceLoader(new JdbcTemplate(ds), txnMgr);
}

// @Bean
// public BlobResourceLoader postgresBlobResourceLoader(DataSource ds, PlatformTransactionManager txnMgr) {
// return new PostgresBlobResourceLoader(new JdbcTemplate(ds), txnMgr);
// }
//
// @Bean
// public BlobResourceLoader mysqlBlobResourceLoader(DataSource ds, PlatformTransactionManager txnMgr) {
// return new MySQLBlobResourceLoader(new JdbcTemplate(ds), txnMgr);
// }
@Bean
public JpaStoreProperties jpaStoreProperties() {
JpaStoreProperties storeProperties = new JpaStorePropertiesImpl();
if (configurers != null) {
for (JpaStoreConfigurer configurer : configurers) {
configurer.configure(storeProperties);
}
}
return storeProperties;
}
}
Expand Up @@ -3,7 +3,6 @@
import internal.org.springframework.content.jpa.io.DelegatingBlobResourceLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.content.commons.repository.factory.AbstractStoreFactoryBean;
import org.springframework.content.jpa.io.BlobResourceLoader;
import org.springframework.util.Assert;

import internal.org.springframework.content.jpa.operations.JpaContentTemplate;
Expand All @@ -23,11 +22,14 @@ public class JpaStoreFactoryBean extends AbstractStoreFactoryBean {
@Autowired
private DelegatingBlobResourceLoader blobResourceLoader;

@Autowired
private JpaStorePropertiesImpl properties;

@Override
protected Object getContentStoreImpl() {
Assert.notNull(template, "template cannot be null");
Assert.notNull(blobResourceLoader, "blobResourceLoader cannot be null");
return new DefaultJpaStoreImpl(blobResourceLoader, 30);
return new DefaultJpaStoreImpl(blobResourceLoader, properties.getCommitTimeout());
}

}
@@ -0,0 +1,18 @@
package internal.org.springframework.content.jpa.config;

import org.springframework.content.jpa.config.JpaStoreProperties;

public class JpaStorePropertiesImpl implements JpaStoreProperties {

private int commitTimeout = 30;

public org.springframework.content.jpa.config.JpaStoreProperties commitTimeout(int seconds) {
commitTimeout = seconds;
return this;
}

public int getCommitTimeout() {
return commitTimeout;
}

}
@@ -0,0 +1,7 @@
package org.springframework.content.jpa.config;

public interface JpaStoreConfigurer {

void configure(JpaStoreProperties store);

}
@@ -0,0 +1,7 @@
package org.springframework.content.jpa.config;

public interface JpaStoreProperties {

JpaStoreProperties commitTimeout(int seconds);

}
Expand Up @@ -68,7 +68,7 @@ public OutputStream getOutputStream() throws IOException {
public void run(){
try {
Object rc = update(txn, is, id, resource);
if (rc != null || rc.equals(-1)) {
if (rc != null && !rc.equals(-1)) {
resource.setId(rc);
}
} catch (SQLException e) {
Expand Down
Expand Up @@ -12,7 +12,11 @@
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
Expand All @@ -28,6 +32,8 @@
import org.springframework.content.commons.repository.ContentStore;
import org.springframework.content.jpa.config.EnableJpaContentRepositories;
import org.springframework.content.jpa.config.EnableJpaStores;
import org.springframework.content.jpa.config.JpaStoreProperties;
import org.springframework.content.jpa.config.JpaStoreConfigurer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -51,6 +57,9 @@
public class EnableJpaStoresTest {

private AnnotationConfigApplicationContext context;

private static JpaStoreConfigurer configurer;

{
Describe("EnableJpaStores", () -> {
Context("given a context and a configuartion with a jpa content repository bean", () -> {
Expand Down Expand Up @@ -93,6 +102,21 @@ public class EnableJpaStoresTest {
}
});
});
Context("given a context with a JpaStoreConfigurer", () -> {
BeforeEach(() -> {
configurer = mock(JpaStoreConfigurer.class);

context = new AnnotationConfigApplicationContext();
context.register(ConfigWithConfigurer.class);
context.refresh();
});
It("should call that configurer to help customize the store", () -> {
verify(configurer).configure(argThat(is(instanceOf(JpaStoreProperties.class))));
});
AfterEach(() -> {
context.close();
});
});
});

Describe("EnableJpaContentRepositores", () -> {
Expand Down Expand Up @@ -128,7 +152,7 @@ public void noop() {
}

@Configuration
@EnableJpaStores(basePackages="contains.no.jpa.repositores")
@EnableJpaStores(basePackages="contains.no.jpa.repositories")
@Import(InfrastructureConfig.class)
public static class EmptyConfig {
}
Expand All @@ -139,6 +163,17 @@ public static class EmptyConfig {
public static class TestConfig {
}

@Configuration
@EnableJpaStores
@Import(InfrastructureConfig.class)
public static class ConfigWithConfigurer {

@Bean
JpaStoreConfigurer configurer() {
return configurer;
}
}

@Configuration
@EnableJpaContentRepositories
@Import(InfrastructureConfig.class)
Expand Down

0 comments on commit d00fb61

Please sign in to comment.