Skip to content

Commit

Permalink
Merge branch 'release/v0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarman committed Feb 11, 2015
2 parents bbe3e45 + acb15bc commit 01ae793
Show file tree
Hide file tree
Showing 12 changed files with 703 additions and 7 deletions.
88 changes: 87 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,87 @@
# skeleton-ws-spring-boot
# Project Skeleton for Spring Boot Web Services

## Acknowledgements

This is a [LEAN**STACKS**](http://www.leanstacks.com) solution.

## Getting Started

This is a project skeleton for a [Spring Boot](http://projects.spring.io/spring-boot/) RESTful web services application.

## Languages

This project is authored in Java.

## Installation

### Fork the Repository

Fork the [Spring Boot web services skeleton project](https://github.com/mwarman/skeleton-ws-spring-boot) on GitHub. Clone the project to the host machine.

### Dependencies

The project requires the following dependencies be installed on the host machine:

* Java Development Kit 7 or later
* Apache Maven 3 or later

## Running

The project uses [Maven](http://maven.apache.org/) for build, package, and test workflow automation. The following Maven goals are the most commonly used.

### spring-boot:run

The `spring-boot:run` Maven goal performs the following workflow steps:

* compiles Java classes to the /target directory
* copies all resources to the /target directory
* starts an embedded Apache Tomcat server

To execute the `spring-boot:run` Maven goal, type the following command at a terminal prompt in the project base directory.

```
mvn spring-boot:run
```

Type `ctrl-C` to halt the web server.

This goal is used for local machine development and functional testing. Use the `package` goal for server deployment.

### package

The `package` Maven goal performs the following workflow steps:

* compiles Java classes to the /target directory
* copies all resources to the /target directory
* prepares an executable JAR file in the /target directory

The `package` Maven goal is designed to prepare the application for distribution to server environments. The application and all dependencies are packaged into a single, executable JAR file.

To execute the `package` goal, type the following command at a terminal prompt in the project base directory.

```
mvn clean package
```

The application distribution artifact is placed in the /target directory and is named using the `artifactId` and `version` from the pom.xml file. To run the JAR file use the following command:

```
java -jar example-1.0.0.jar
```

### test

The `test` Maven goal performs the following workflow steps:

* compiles Java classes to the /target directory
* copies all resources to the /target directory
* executes the unit test suites
* produces unit test reports

The `test` Maven goal is designed to allow engineers the means to run the unit test suites against the main source code. This goal may also be used on continuous integration servers such as Jenkins, etc.

To execute the `test` Maven goal, type the following command at a terminal prompt in the project base directory.

```
mvn clean test
```
23 changes: 22 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.leanstacks</groupId>
<artifactId>skeleton-ws-spring-boot</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
Expand All @@ -18,6 +18,27 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Dependencies for GuavaCacheManager -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>

<build>
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/com/leanstacks/ws/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,39 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
* Spring Boot Main Application Class. Entry point for the application.
* Spring Boot main application class.
*
* @author Matt Warman
*/
@SpringBootApplication
public class Application
{
public static void main( String[] args ) throws Exception
{
@EnableTransactionManagement
@EnableCaching
@EnableScheduling
public class Application {

/**
* Entry point for the application.
* @param args Command line arguments.
* @throws Exception Thrown when an unexpected exception is thrown from the
* application.
*/
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}

@Bean
public CacheManager cacheManager() {
GuavaCacheManager cacheManager = new GuavaCacheManager("greetings");

return cacheManager;
}

}
132 changes: 132 additions & 0 deletions src/main/java/com/leanstacks/ws/batch/GreetingBatchBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.leanstacks.ws.batch;

import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.leanstacks.ws.model.Greeting;
import com.leanstacks.ws.service.GreetingService;

/**
* The GreetingBatchBean contains <code>@Scheduled</code> methods operating on
* Greeting entities to perform batch operations.
*
* @author Matt Warman
*/
@Component
public class GreetingBatchBean {

private Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
private GreetingService greetingService;

/**
* Use a cron expression to execute logic on a schedule. Expression: second
* minute hour day-of-month month weekday
*
* @see http
* ://docs.spring.io/spring/docs/current/javadoc-api/org/springframework
* /scheduling/support/CronSequenceGenerator.html
*/
@Scheduled(
cron = "${batch.greeting.cron}")
public void cronJob() {
logger.info("> cronJob");

// Add scheduled logic here

Collection<Greeting> greetings = greetingService.findAll();
logger.info("There are {} greetings in the data store.",
greetings.size());

logger.info("< cronJob");
}

/**
* Execute logic beginning at fixed intervals. Use the
* <code>fixedRate</code> element to indicate how frequently the method is
* to be invoked.
*/
@Scheduled(
fixedRateString = "${batch.greeting.fixedrate}")
public void fixedRateJob() {
logger.info("> fixedRateJob");

// Add scheduled logic here

Collection<Greeting> greetings = greetingService.findAll();
logger.info("There are {} greetings in the data store.",
greetings.size());

logger.info("< fixedRateJob");
}

/**
* Execute logic beginning at fixed intervals with a delay after the
* application starts. Use the <code>fixedRate</code> element to indicate
* how frequently the method is to be invoked. Use the
* <code>initialDelay</code> element to indicate how long to wait after
* application startup to schedule the first execution.
*/
@Scheduled(
initialDelayString = "${batch.greeting.initialdelay}",
fixedRateString = "${batch.greeting.fixedrate}")
public void fixedRateJobWithInitialDelay() {
logger.info("> fixedRateJobWithInitialDelay");

// Add scheduled logic here

Collection<Greeting> greetings = greetingService.findAll();
logger.info("There are {} greetings in the data store.",
greetings.size());

logger.info("< fixedRateJobWithInitialDelay");
}

/**
* Execute logic with a delay between the end of the last execution and the
* beginning of the next. Use the <code>fixedDelay</code> element to
* indicate the time to wait between executions.
*/
@Scheduled(
fixedDelayString = "${batch.greeting.fixeddelay}")
public void fixedDelayJob() {
logger.info("> fixedDelayJob");

// Add scheduled logic here

Collection<Greeting> greetings = greetingService.findAll();
logger.info("There are {} greetings in the data store.",
greetings.size());

logger.info("< fixedDelayJob");
}

/**
* Execute logic with a delay between the end of the last execution and the
* beginning of the next. Use the <code>fixedDelay</code> element to
* indicate the time to wait between executions. Use the
* <code>initialDelay</code> element to indicate how long to wait after
* application startup to schedule the first execution.
*/
@Scheduled(
initialDelayString = "${batch.greeting.initialdelay}",
fixedDelayString = "${batch.greeting.fixeddelay}")
public void fixedDelayJobWithInitialDelay() {
logger.info("> fixedDelayJobWithInitialDelay");

// Add scheduled logic here

Collection<Greeting> greetings = greetingService.findAll();
logger.info("There are {} greetings in the data store.",
greetings.size());

logger.info("< fixedDelayJobWithInitialDelay");
}

}
43 changes: 43 additions & 0 deletions src/main/java/com/leanstacks/ws/model/Greeting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.leanstacks.ws.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;

/**
* The Greeting class is an entity model object.
*
* @author Matt Warman
*/
@Entity
public class Greeting {

@Id
@GeneratedValue
private Long id;

@NotNull
private String text;

public Greeting() {

}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

}
11 changes: 11 additions & 0 deletions src/main/java/com/leanstacks/ws/repository/GreetingRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.leanstacks.ws.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.leanstacks.ws.model.Greeting;

@Repository
public interface GreetingRepository extends JpaRepository<Greeting, Long> {

}
Loading

0 comments on commit 01ae793

Please sign in to comment.