Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
nschwalbe committed Dec 18, 2018
2 parents f53ab17 + dca651c commit 48283f3
Show file tree
Hide file tree
Showing 16 changed files with 979 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true


[*]

# Change these settings to your own preference
indent_style = space
indent_size = 4

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[*.yml]
indent_style = space
indent_size = 2
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

.idea/
*.iml
target/

104 changes: 104 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Post Office

Bring your mail to the post office. It will be stored and a postal worker will deliver it.
Because of the always recurring task in every project to store a mail and send it out with a worker I extracted this as a
module.

This module is auto configured and depends on spring mail.

Currently only MongoDB is supported. For other databases implement the `MailStorage` interface.

## Installation

[![Release](https://jitpack.io/v/nschwalbe/postoffice.svg?style=flat-square)](https://jitpack.io/#nschwalbe/postoffice)

The module is build by jitpack and can be downloaded with maven or gradle.

Add the jitpack repository:

```xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
```

And dependency:

```xml
<dependency>
<groupId>com.github.nschwalbe</groupId>
<artifactId>postoffice</artifactId>
<version>develop-SNAPSHOT</version>
</dependency>
```

For more information like gradle see here: <https://jitpack.io/#nschwalbe/postoffice>


## Configuration
### Mail Server
Configure at least the `spring.mail.host` property. For more information see the spring mail configuration.

<http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-email.html>

### Thread Pool
The default `ThreadPoolTaskScheduler` is used which comes by default with only one thread.
To configure the scheduler define a configuration class which implements `SchedulingConfigurer`.
For example:

```java
@Configuration
@EnableScheduling
public class TaskExecutionConfig implements SchedulingConfigurer {

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(3);
threadPoolTaskScheduler.initialize();
taskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
}
}
```

## Usage

Define a Mailer class in your project and inject the `MailService`.

```java
@Component
public class MyMailer {

private final PostOffice postOffice;
private final TemplateEngine templateEngine;
private final MessageSource messageSource;

@Autowired
public MyMailer(PostOffice postOffice, TemplateEngine templateEngine, Environment environment, MessageSource messageSource) {
this.postOffice = postOffice;
this.templateEngine = templateEngine;
this.messageSource = messageSource;
}

public void sendMail() {

Context ctx = createContext();

try {
String subject = createSubject();
String content = createContent(ctx);

MimeMessage mimeMessage = postOffice.createMimeMessage(subject, from, to, content, true);

postOffice.postMail(mimeMessage);

} catch (Exception e) {
log.error("Could not create mail!", e);
}
}
}
```

105 changes: 105 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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>de.nschwalbe</groupId>
<artifactId>postoffice</artifactId>
<version>0.1.3</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<optional>true</optional>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.5.3</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
36 changes: 36 additions & 0 deletions src/main/java/de/nschwalbe/postoffice/MailAddress.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package de.nschwalbe.postoffice;

import java.util.Objects;

/**
* A mail address.
*
* @author Nathanael Schwalbe
* @since 14.04.2017
*/
public class MailAddress {

private final String address;
private final String personal;

private MailAddress(String address, String personal) {
this.address = Objects.requireNonNull(address);
this.personal = personal;
}

public static MailAddress of(String address, String personal) {
return new MailAddress(address, personal);
}

public static MailAddress of(String address) {
return new MailAddress(address, null);
}

public String getAddress() {
return address;
}

public String getPersonal() {
return personal;
}
}
13 changes: 13 additions & 0 deletions src/main/java/de/nschwalbe/postoffice/MailProcessState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.nschwalbe.postoffice;

/**
* Defines processing state of an mail.
*
* @author Nathanael Schwalbe
* @since 05.04.2017
*/
public enum MailProcessState {

NOT_SENT, IN_PROGRESS, FAILED, SENT

}
22 changes: 22 additions & 0 deletions src/main/java/de/nschwalbe/postoffice/MailStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.nschwalbe.postoffice;

import java.util.List;

/**
* Persists a mail to be send by a scheduled task.
*
* @author Nathanael Schwalbe
* @since 05.04.2017
*/
public interface MailStorage {

PersistedMail create(byte[] mimeMessageContent);

List<String> findNotSentIds();

PersistedMail findNotSentAndStartProgress(String mailId);

void delete(String id);

void update(PersistedMail mail);
}
25 changes: 25 additions & 0 deletions src/main/java/de/nschwalbe/postoffice/PersistedMail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.nschwalbe.postoffice;

import java.time.LocalDateTime;

/**
* Defines a mail to be stored in a database.
*
* @author Nathanael Schwalbe
* @since 05.04.2017
*/
public interface PersistedMail {

String getId();

LocalDateTime getCreatedDate();
LocalDateTime getLastModifiedDate();

byte[] getMimeMessageContent();

MailProcessState getState();
void setState(MailProcessState state);

String getErrorMessage();
void setErrorMessage(String message);
}
Loading

0 comments on commit 48283f3

Please sign in to comment.