Skip to content

Encon handler #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codestyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ limitations under the License.
<module name="OperatorWrap">
<property name="option" value="NL"/>
<property name="tokens"
value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LT, MINUS, MOD, NOT_EQUAL, QUESTION, SL, SR, STAR"/>
value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LE, LITERAL_INSTANCEOF, LT, MINUS, MOD, NOT_EQUAL, QUESTION, SL, SR, STAR"/>
</module>
<module name="ParenPad"/>
<module name="SeparatorWrap">
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Turn on checkstyle JavaDocs module.
- Add updates to the protocol, like new `ControlMessage`.
- Add Spring Boot support.
- Remove non blocking queues, because it doesn't guarantee the order.

## [1.4.0](https://github.com/appulse-projects/encon-java/releases/tag/1.4.0) - 2018-07-07

Add `encon-handler`.

### Changed

- Add `asText` implementation for `ErlangBinary`.
- Update dependencies.

## [1.3.1](https://github.com/appulse-projects/encon-java/releases/tag/1.3.1) - 2018-06-28

Expand Down
2 changes: 1 addition & 1 deletion encon-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ limitations under the License.
<parent>
<groupId>io.appulse.encon</groupId>
<artifactId>encon-parent</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
</parent>

<artifactId>encon-common</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions encon-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ First of all, add config's dependency:
<dependency>
<groupId>io.appulse.encon</groupId>
<artifactId>encon-config</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
</dependency>
...
</dependencies>
Expand All @@ -23,7 +23,7 @@ First of all, add config's dependency:
**Gradle**:

```groovy
compile 'io.appulse.encon.java:encon-config:1.3.1'
compile 'io.appulse.encon.java:encon-config:1.4.0'
```

### File based configuration
Expand Down
2 changes: 1 addition & 1 deletion encon-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ limitations under the License.
<parent>
<groupId>io.appulse.encon</groupId>
<artifactId>encon-parent</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
</parent>

<artifactId>encon-config</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions encon-databind/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ First of all, add databind's dependency:
<dependency>
<groupId>io.appulse.encon</groupId>
<artifactId>encon-databind</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
</dependency>
...
</dependencies>
Expand All @@ -23,7 +23,7 @@ First of all, add databind's dependency:
**Gradle**:

```groovy
compile 'io.appulse.encon.java:encon-databind:1.3.1'
compile 'io.appulse.encon.java:encon-databind:1.4.0'
```

Let's imagine, you have POJO like this:
Expand Down
2 changes: 1 addition & 1 deletion encon-databind/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ limitations under the License.
<parent>
<groupId>io.appulse.encon</groupId>
<artifactId>encon-parent</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
</parent>

<artifactId>encon-databind</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ public static <T> T deserialize (@NonNull ErlangTerm container, @NonNull Class<T
if (ErlangTerm.class.isAssignableFrom(type)) {
return (T) container;
}
PojoDescriptor descriptor = PojoParser.parse(type);
Deserializer<?> deserializer = descriptor.getDeserializer();

Deserializer<?> deserializer = Deserializer.findInPredefined(type);
if (deserializer == null) {
PojoDescriptor descriptor = PojoParser.parse(type);
deserializer = descriptor.getDeserializer();
}
return (T) deserializer.deserialize(container);
}

Expand All @@ -67,8 +71,12 @@ public static ErlangTerm serialize (@NonNull Object object) {
return (ErlangTerm) object;
}
val type = object.getClass();
PojoDescriptor descriptor = PojoParser.parse(type);
Serializer<?> serializer = descriptor.getSerializer();

Serializer<?> serializer = Serializer.findInPredefined(type);
if (serializer == null) {
PojoDescriptor descriptor = PojoParser.parse(type);
serializer = descriptor.getSerializer();
}
return serializer.serializeUntyped(object);
}

Expand Down
128 changes: 128 additions & 0 deletions encon-handler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Overview

Different classes for convenient mailbox handling.

## Usage

First of all, add dependency:

**Maven**:

```xml
<dependencies>
...
<dependency>
<groupId>io.appulse.encon</groupId>
<artifactId>encon-handler</artifactId>
<version>1.4.0</version>
</dependency>
...
</dependencies>
```

**Gradle**:

```groovy
compile 'io.appulse.encon.java:encon-handler:1.4.0'
```

### Basics

Let's create a `dummy` message handler implementation:

```java

import io.appulse.encon.connection.control.ControlMessage;
import io.appulse.encon.handler.message.MessageHandler;
import io.appulse.encon.mailbox.Mailbox;
import io.appulse.encon.terms.ErlangTerm;



class DummyMessageHandler implements MessageHandler {

@Override
public void handle (Mailbox self, ControlMessage header, ErlangTerm body) {
System.out.println("A new message");
}
}
```

Now we can use it in one of the prepared `MailboxHandler` implementation:

```java

import io.appulse.encon.handler.mailbox.BlockingMailboxHandler;
import io.appulse.encon.handler.mailbox.MailboxHandler;



MailboxHandler handler = BlockingMailboxHandler.builder()
.messageHandler(new DummyMessageHandler())
.mailbox(myMailbox)
.build();

// start it in a separate thread:
handler.startExecutor();
// ...
handler.close();
```

### Advanced

Imagine, you have two services:

```java

public static class MyService1 {

public void handler1 () {
// ...
}

public void handler2 (int a, String b, boolean c) {
// ...
}
}

public static class MyService2 {

public void popa (int a) throws Exception {
// ...
}
}
```

And now, you would like to route incoming messages to those services. You can do it the following way:

```java

import static io.appulse.encon.handler.message.matcher.Matchers.anyString;
import static io.appulse.encon.handler.message.matcher.Matchers.anyInt;
import static io.appulse.encon.handler.message.matcher.Matchers.eq;

import io.appulse.encon.handler.message.MessageHandler;
import io.appulse.encon.handler.message.matcher.MethodMatcherMessageHandler;



MyService1 service1 = new MyService1();
MyService2 service2 = new MyService2();

MessageHandler handler = MethodMatcherMessageHandler.builder()
.wrap(service1)
// redirects '[]' (empty list) to method MyService1.handler1
.list(it -> it.handler1())
// redirects tuple {any_number, any_string, atom(false)}
// to MyService1.handler2
.tuple(it -> it.handler2(anyInt(), anyString(), eq(false)))
.wrap(service2)
// redirects {42} to MyService2.popa
.tuple(it -> it.popa(42))
.build();

// start it in a separate thread:
handler.startExecutor();
// ...
handler.close();
```
116 changes: 116 additions & 0 deletions encon-handler/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Copyright 2018 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<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>

<parent>
<groupId>io.appulse.encon</groupId>
<artifactId>encon-parent</artifactId>
<version>1.4.0</version>
</parent>

<artifactId>encon-handler</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>encon</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>encon-databind</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.7</version>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Loading