diff --git a/.github/workflows/build-site.yml b/.github/workflows/build-site.yml new file mode 100644 index 0000000..cb9a0ce --- /dev/null +++ b/.github/workflows/build-site.yml @@ -0,0 +1,13 @@ +# This workflow takes the contents of the branches/tags and builds the production documentation site +name: Build production site + +on: + push: + branches: [master] + +jobs: + dispatch: + runs-on: ubuntu-latest + steps: + - name: Trigger build + run: curl -X POST -d {} https://api.netlify.com/build_hooks/6238ac2881e6d20c7db8e6c8 diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 930999a..7d45407 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,10 +2,10 @@ name: build on: push: paths-ignore: - - 'doc/**' + - 'docs/**' pull_request: paths-ignore: - - 'doc/**' + - 'docs/**' jobs: build: diff --git a/.github/workflows/guide-website-update.yml b/.github/workflows/guide-website-update.yml deleted file mode 100644 index 49c4541..0000000 --- a/.github/workflows/guide-website-update.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: Guide Web Site Deploy - -on: - push: - branches: master - paths: - - 'doc/**' - -jobs: - antora: - runs-on: ubuntu-18.04 - steps: - - uses: actions/checkout@v2.3.4 - with: - repository: hazelcast-guides/guides-site - ref: master - token: ${{ secrets.SECRET_TOKEN }} - - - name: Install Antora - run: | - sudo apt -y install curl dirmngr apt-transport-https lsb-release ca-certificates - curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - - sudo apt -y install nodejs - sudo npm i -g @antora/cli@2.3 @antora/site-generator-default@2.3 - - - name: Build website artifacts - run: | - sh create.sh - - name: Commit artifacts changes - run: | - git config --global user.name 'devOpsHazelcast' - git config --global user.email 'devops@hazelcast.com' - export GUIDE_REPO=$(cut -d/ -f2 <<<"${GITHUB_REPOSITORY}") - export COMMIT_ID=$(git rev-parse --short "$GITHUB_SHA") - git add docs - git commit -m "${GUIDE_REPO}-${COMMIT_ID} guide update" - - - name: Push artifacts to guides-site repo - run: git push diff --git a/README.adoc b/README.adoc new file mode 100644 index 0000000..5cb2543 --- /dev/null +++ b/README.adoc @@ -0,0 +1 @@ +See the link:https://docs.hazelcast.com/tutorials/springboot-hibernate[tutorial]. diff --git a/README.md b/README.md deleted file mode 100644 index 6521581..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -See the guide [here](https://guides.hazelcast.org/springboot-hibernate). diff --git a/doc/antora.yml b/doc/antora.yml deleted file mode 100644 index 04837de..0000000 --- a/doc/antora.yml +++ /dev/null @@ -1,3 +0,0 @@ -name: springboot-hibernate -title: Getting Started with Hazelcast and Hibernate Second-Level Cache -version: master diff --git a/doc/modules/ROOT/pages/index.adoc b/doc/modules/ROOT/pages/index.adoc deleted file mode 100644 index 2e30600..0000000 --- a/doc/modules/ROOT/pages/index.adoc +++ /dev/null @@ -1,129 +0,0 @@ -:github-address: https://github.com/hazelcast-guides/springboot-hibernate -:templates-url: templates:ROOT:page$/ -:hazelcast: Hazelcast IMDG -:framework: Spring Boot - -= Getting Started with Hazelcast and Hibernate Second-Level Cache - -This guide will get you started to use Hazelcast in a Spring Boot application. You can see the code sample {github-address}[here]. - -== What You’ll Learn - -In this guide, you'll see how to quickly set up a Spring Boot application using Hazelcast as Hibernate's second-level cache. - -== Prerequisites - -- ~10 minutes -include::{templates-url}/microservices/prerequisites.adoc[] - -In order to run the code sample, make sure to have PostgresSQL database accessible and configured properly in the `application.properties` file. - -[TIP] -==== -You can spin-up a PostgreSQL instance easily using Docker: ----- -docker run --name 2lc-postgres --publish 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres:13 ----- -==== - -== Configuration - -In order to enable JPA, you need to add a dedicated Spring Boot Starter: - -[source,xml] ----- - - org.springframework.boot - spring-boot-starter-data-jpa - ----- - -In order to configure Hazelcast as second-level cache provider, you need to add two dependencies: - -[source,xml] ----- - - com.hazelcast - hazelcast-hibernate53 - ${hazelcast-hibernate.version} - - - - com.hazelcast - hazelcast - ${hazelcast.version} - ----- - -And then, we need to configure Hazelcast IMDG local member setting by adding a standard `hazelcast.xml` file into `src/main/resources`. - -Keep in mind that this might trigger the autoconfiguration of another Hazelcast member so you might want to disable Hazelcast autoconfiguration: - -[source,java] ----- -@SpringBootApplication(exclude = HazelcastAutoConfiguration.class) ----- - -The last step involves turning on second-level cache by adding two properties into `application.properties` file: - -[source,properties] ----- -spring.jpa.properties.hibernate.cache.use_second_level_cache=true -spring.jpa.properties.hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastCacheRegionFactory ----- - -And now, once you annotate your entity as `@Cacheable`, it will be cached in Hazelcast member: - -[source,java] ----- -@Entity -@Cache(usage = CacheConcurrencyStrategy.READ_WRITE) -public class Book { ... } ----- - -== Running the Application - -Run the application by executing the following command. - ----- -mvn spring-boot:run ----- - -You should see in logs that Hazelcast members started successfully. - ----- -Members {size:1, ver:1} [ - Member [172.21.28.181]:5701 - f3984396-0ec9-40c8-861e-cb34f14d7204 this -] ----- - -What happens then is that an object is saved to the database and then fetched. In the meantime the object is stored in the second level cache. `SecondLevelCacheVisualizer` prints out the L2C content in every 10 seconds. - ----- -20:56:46.937644 -size: 1 - com.hazelcast.hibernate.springhibernate2lc.persistence.Book#1:read-write Item(CacheEntry(com.hazelcast.hibernate.springhibernate2lc.persistence.Book)) - -20:56:56.997365 -size: 1 - com.hazelcast.hibernate.springhibernate2lc.persistence.Book#1:read-write Item(CacheEntry(com.hazelcast.hibernate.springhibernate2lc.persistence.Book)) ----- - -The object is then evicted from the L2C after 30 seconds, which is the configured time-to-live in `hazelcast.xml`. - ----- -20:57:17.028600 -size: 0 ----- - -== Summary - -In this guide, we bootstrapped a Spring Boot application which uses Hazelcast as Hibernate's second-level cache. - - -== See Also - -- xref:hibernate-jcache:ROOT:index.adoc[Using JCache for Hibernate L2C] -- xref:hazelcast-embedded-springboot:ROOT:index.adoc[Hazelcast in SpringBoot] -- xref:kubernetes-embedded:ROOT:index.adoc[Hazelcast with SpringBoot in Kubernetes] - diff --git a/docs/antora.yml b/docs/antora.yml new file mode 100644 index 0000000..2b5b7d4 --- /dev/null +++ b/docs/antora.yml @@ -0,0 +1,2 @@ +name: tutorials +version: ~ diff --git a/docs/modules/ROOT/examples/springboot-hibernate/pom.xml b/docs/modules/ROOT/examples/springboot-hibernate/pom.xml new file mode 120000 index 0000000..ed314d5 --- /dev/null +++ b/docs/modules/ROOT/examples/springboot-hibernate/pom.xml @@ -0,0 +1 @@ +../../../../../pom.xml \ No newline at end of file diff --git a/docs/modules/ROOT/examples/springboot-hibernate/src b/docs/modules/ROOT/examples/springboot-hibernate/src new file mode 120000 index 0000000..d753b57 --- /dev/null +++ b/docs/modules/ROOT/examples/springboot-hibernate/src @@ -0,0 +1 @@ +../../../../../src \ No newline at end of file diff --git a/docs/modules/ROOT/pages/springboot-hibernate.adoc b/docs/modules/ROOT/pages/springboot-hibernate.adoc new file mode 100644 index 0000000..414db6c --- /dev/null +++ b/docs/modules/ROOT/pages/springboot-hibernate.adoc @@ -0,0 +1,107 @@ += Get Started with Hazelcast and Hibernate Second-Level Cache +:templates-url: templates:ROOT:page$/ +:page-layout: tutorial +:page-product: imdg +:page-categories: Caching, Getting Started, Hibernate +:page-lang: java +:page-edition: +:page-est-time: 10 mins +:framework: Spring Boot +:description: In this tutorial, you'll learn how to quickly set up a Spring Boot application using Hazelcast as a Hibernate second-level cache. + +== What You’ll Learn + +{description} + +== Before you Begin + +include::{templates-url}/microservices/prerequisites.adoc[] + +In order to run the code sample, make sure to have PostgresSQL database accessible and configured properly in the `application.properties` file. + +[TIP] +==== +You can spin-up a PostgreSQL instance easily using Docker: +---- +docker run --name 2lc-postgres --publish 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres:13 +---- +==== + +== Configuration + +In order to enable JPA, you need to add a dedicated Spring Boot Starter: + +[source,xml,indent=0] +---- +include::ROOT:example$springboot-hibernate/pom.xml[tag=spring-boot-starter] +---- + +In order to configure Hazelcast as a second-level cache provider, you need to add two dependencies: + +[source,xml,indent=0] +---- +include::ROOT:example$springboot-hibernate/pom.xml[tags=hazelcast-hibernate;hazelcast-dep] +---- + +And then, you need to configure the Hazelcast member setting by adding a standard `hazelcast.xml` file into `src/main/resources` directory. + +Keep in mind that this might trigger the autoconfiguration of another Hazelcast member so you might want to disable Hazelcast autoconfiguration: + +[source,java,indent=0] +---- +include::ROOT:example$springboot-hibernate/src/main/java/com/hazelcast/hibernate/springhibernate2lc/SpringHibernate2lcApplication.java[tag=doc-exclude] +---- + +The last step involves turning on second-level cache by adding two properties in the `application.properties` file: + +[source,properties] +---- +include::ROOT:example$springboot-hibernate/src/main/resources/application.properties[tag=doc-second-level] +---- + +And now, after you annotate your entity as `@Cacheable`, it will be cached in Hazelcast member: + +[source,java] +---- +include::ROOT:example$springboot-hibernate/src/main/java/com/hazelcast/hibernate/springhibernate2lc/persistence/Book.java[tag=doc-cachable] +---- + +== Running the Application + +Run the application by executing the following command. + +---- +mvn spring-boot:run +---- + +You should see in logs that Hazelcast members started successfully. + +---- +Members {size:1, ver:1} [ + Member [172.21.28.181]:5701 - f3984396-0ec9-40c8-861e-cb34f14d7204 this +] +---- + +What happens then is that an object is saved to the database and then fetched. In the meantime, the object is stored in the second level cache. `SecondLevelCacheVisualizer` prints out the L2C content in every 10 seconds. + +---- +20:56:46.937644 +size: 1 + com.hazelcast.hibernate.springhibernate2lc.persistence.Book#1:read-write Item(CacheEntry(com.hazelcast.hibernate.springhibernate2lc.persistence.Book)) + +20:56:56.997365 +size: 1 + com.hazelcast.hibernate.springhibernate2lc.persistence.Book#1:read-write Item(CacheEntry(com.hazelcast.hibernate.springhibernate2lc.persistence.Book)) +---- + +The object is then evicted from the L2C after 30 seconds, which is the configured time-to-live in `hazelcast.xml`. + +---- +20:57:17.028600 +size: 0 +---- + +== Summary + +In this guide, you bootstrapped a Spring Boot application to use Hazelcast as a Hibernate second-level cache. + diff --git a/pom.xml b/pom.xml index 279a087..7b6be85 100644 --- a/pom.xml +++ b/pom.xml @@ -31,10 +31,12 @@ + org.springframework.boot spring-boot-starter-data-jpa + org.springframework.boot @@ -47,18 +49,20 @@ - + com.hazelcast hazelcast-hibernate53 ${hazelcast-hibernate.version} - + + com.hazelcast hazelcast ${hazelcast.version} + org.postgresql diff --git a/src/main/java/com/hazelcast/hibernate/springhibernate2lc/SpringHibernate2lcApplication.java b/src/main/java/com/hazelcast/hibernate/springhibernate2lc/SpringHibernate2lcApplication.java index 5c6edc1..7b23728 100644 --- a/src/main/java/com/hazelcast/hibernate/springhibernate2lc/SpringHibernate2lcApplication.java +++ b/src/main/java/com/hazelcast/hibernate/springhibernate2lc/SpringHibernate2lcApplication.java @@ -6,7 +6,10 @@ import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling // needed just for the sake of SecondLevelCacheVisualizer -@SpringBootApplication(exclude = HazelcastAutoConfiguration.class) // in order to avoid autoconfiguring an extra Hazelcast instance +// in order to avoid autoconfiguring an extra Hazelcast instance +// tag::doc-exclude[] +@SpringBootApplication(exclude = HazelcastAutoConfiguration.class) +// end::doc-exclude[] public class SpringHibernate2lcApplication { public static void main(String[] args) { diff --git a/src/main/java/com/hazelcast/hibernate/springhibernate2lc/persistence/Book.java b/src/main/java/com/hazelcast/hibernate/springhibernate2lc/persistence/Book.java index 2a86d17..c5ae36f 100644 --- a/src/main/java/com/hazelcast/hibernate/springhibernate2lc/persistence/Book.java +++ b/src/main/java/com/hazelcast/hibernate/springhibernate2lc/persistence/Book.java @@ -7,9 +7,11 @@ import javax.persistence.GeneratedValue; import javax.persistence.Id; +// tag::doc-cachable[] @Entity @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class Book { +// end::doc-cachable[] @Id @GeneratedValue diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4a93c3b..050ea88 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -10,8 +10,10 @@ spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.properties.hibernate.show_sql=true # Second-Level Cache Configuration +# tag::doc-second-level[] spring.jpa.properties.hibernate.cache.use_second_level_cache=true spring.jpa.properties.hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastCacheRegionFactory +# end::doc-second-level[] # spring.jpa.properties.hibernate.cache.hazelcast.use_native_client=true logging.level.org.hibernate.type=trace \ No newline at end of file