Skip to content

Commit ff4534d

Browse files
authored
Encon handler (#6)
* Add encon-handler sub-project * Edit docs
1 parent fe5fb80 commit ff4534d

File tree

32 files changed

+2241
-21
lines changed

32 files changed

+2241
-21
lines changed

.codestyle/checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ limitations under the License.
233233
<module name="OperatorWrap">
234234
<property name="option" value="NL"/>
235235
<property name="tokens"
236-
value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LT, MINUS, MOD, NOT_EQUAL, QUESTION, SL, SR, STAR"/>
236+
value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LE, LITERAL_INSTANCEOF, LT, MINUS, MOD, NOT_EQUAL, QUESTION, SL, SR, STAR"/>
237237
</module>
238238
<module name="ParenPad"/>
239239
<module name="SeparatorWrap">

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2222
- Turn on checkstyle JavaDocs module.
2323
- Add updates to the protocol, like new `ControlMessage`.
2424
- Add Spring Boot support.
25+
- Remove non blocking queues, because it doesn't guarantee the order.
26+
27+
## [1.4.0](https://github.com/appulse-projects/encon-java/releases/tag/1.4.0) - 2018-07-07
28+
29+
Add `encon-handler`.
30+
31+
### Changed
32+
33+
- Add `asText` implementation for `ErlangBinary`.
34+
- Update dependencies.
2535

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

encon-common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ limitations under the License.
2525
<parent>
2626
<groupId>io.appulse.encon</groupId>
2727
<artifactId>encon-parent</artifactId>
28-
<version>1.3.1</version>
28+
<version>1.4.0</version>
2929
</parent>
3030

3131
<artifactId>encon-common</artifactId>

encon-config/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ First of all, add config's dependency:
1414
<dependency>
1515
<groupId>io.appulse.encon</groupId>
1616
<artifactId>encon-config</artifactId>
17-
<version>1.3.1</version>
17+
<version>1.4.0</version>
1818
</dependency>
1919
...
2020
</dependencies>
@@ -23,7 +23,7 @@ First of all, add config's dependency:
2323
**Gradle**:
2424

2525
```groovy
26-
compile 'io.appulse.encon.java:encon-config:1.3.1'
26+
compile 'io.appulse.encon.java:encon-config:1.4.0'
2727
```
2828

2929
### File based configuration

encon-config/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ limitations under the License.
2525
<parent>
2626
<groupId>io.appulse.encon</groupId>
2727
<artifactId>encon-parent</artifactId>
28-
<version>1.3.1</version>
28+
<version>1.4.0</version>
2929
</parent>
3030

3131
<artifactId>encon-config</artifactId>

encon-databind/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ First of all, add databind's dependency:
1414
<dependency>
1515
<groupId>io.appulse.encon</groupId>
1616
<artifactId>encon-databind</artifactId>
17-
<version>1.3.1</version>
17+
<version>1.4.0</version>
1818
</dependency>
1919
...
2020
</dependencies>
@@ -23,7 +23,7 @@ First of all, add databind's dependency:
2323
**Gradle**:
2424

2525
```groovy
26-
compile 'io.appulse.encon.java:encon-databind:1.3.1'
26+
compile 'io.appulse.encon.java:encon-databind:1.4.0'
2727
```
2828

2929
Let's imagine, you have POJO like this:

encon-databind/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ limitations under the License.
2525
<parent>
2626
<groupId>io.appulse.encon</groupId>
2727
<artifactId>encon-parent</artifactId>
28-
<version>1.3.1</version>
28+
<version>1.4.0</version>
2929
</parent>
3030

3131
<artifactId>encon-databind</artifactId>

encon-databind/src/main/java/io/appulse/encon/databind/TermMapper.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ public static <T> T deserialize (@NonNull ErlangTerm container, @NonNull Class<T
5050
if (ErlangTerm.class.isAssignableFrom(type)) {
5151
return (T) container;
5252
}
53-
PojoDescriptor descriptor = PojoParser.parse(type);
54-
Deserializer<?> deserializer = descriptor.getDeserializer();
53+
54+
Deserializer<?> deserializer = Deserializer.findInPredefined(type);
55+
if (deserializer == null) {
56+
PojoDescriptor descriptor = PojoParser.parse(type);
57+
deserializer = descriptor.getDeserializer();
58+
}
5559
return (T) deserializer.deserialize(container);
5660
}
5761

@@ -67,8 +71,12 @@ public static ErlangTerm serialize (@NonNull Object object) {
6771
return (ErlangTerm) object;
6872
}
6973
val type = object.getClass();
70-
PojoDescriptor descriptor = PojoParser.parse(type);
71-
Serializer<?> serializer = descriptor.getSerializer();
74+
75+
Serializer<?> serializer = Serializer.findInPredefined(type);
76+
if (serializer == null) {
77+
PojoDescriptor descriptor = PojoParser.parse(type);
78+
serializer = descriptor.getSerializer();
79+
}
7280
return serializer.serializeUntyped(object);
7381
}
7482

encon-handler/README.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Overview
2+
3+
Different classes for convenient mailbox handling.
4+
5+
## Usage
6+
7+
First of all, add dependency:
8+
9+
**Maven**:
10+
11+
```xml
12+
<dependencies>
13+
...
14+
<dependency>
15+
<groupId>io.appulse.encon</groupId>
16+
<artifactId>encon-handler</artifactId>
17+
<version>1.4.0</version>
18+
</dependency>
19+
...
20+
</dependencies>
21+
```
22+
23+
**Gradle**:
24+
25+
```groovy
26+
compile 'io.appulse.encon.java:encon-handler:1.4.0'
27+
```
28+
29+
### Basics
30+
31+
Let's create a `dummy` message handler implementation:
32+
33+
```java
34+
35+
import io.appulse.encon.connection.control.ControlMessage;
36+
import io.appulse.encon.handler.message.MessageHandler;
37+
import io.appulse.encon.mailbox.Mailbox;
38+
import io.appulse.encon.terms.ErlangTerm;
39+
40+
41+
42+
class DummyMessageHandler implements MessageHandler {
43+
44+
@Override
45+
public void handle (Mailbox self, ControlMessage header, ErlangTerm body) {
46+
System.out.println("A new message");
47+
}
48+
}
49+
```
50+
51+
Now we can use it in one of the prepared `MailboxHandler` implementation:
52+
53+
```java
54+
55+
import io.appulse.encon.handler.mailbox.BlockingMailboxHandler;
56+
import io.appulse.encon.handler.mailbox.MailboxHandler;
57+
58+
59+
60+
MailboxHandler handler = BlockingMailboxHandler.builder()
61+
.messageHandler(new DummyMessageHandler())
62+
.mailbox(myMailbox)
63+
.build();
64+
65+
// start it in a separate thread:
66+
handler.startExecutor();
67+
// ...
68+
handler.close();
69+
```
70+
71+
### Advanced
72+
73+
Imagine, you have two services:
74+
75+
```java
76+
77+
public static class MyService1 {
78+
79+
public void handler1 () {
80+
// ...
81+
}
82+
83+
public void handler2 (int a, String b, boolean c) {
84+
// ...
85+
}
86+
}
87+
88+
public static class MyService2 {
89+
90+
public void popa (int a) throws Exception {
91+
// ...
92+
}
93+
}
94+
```
95+
96+
And now, you would like to route incoming messages to those services. You can do it the following way:
97+
98+
```java
99+
100+
import static io.appulse.encon.handler.message.matcher.Matchers.anyString;
101+
import static io.appulse.encon.handler.message.matcher.Matchers.anyInt;
102+
import static io.appulse.encon.handler.message.matcher.Matchers.eq;
103+
104+
import io.appulse.encon.handler.message.MessageHandler;
105+
import io.appulse.encon.handler.message.matcher.MethodMatcherMessageHandler;
106+
107+
108+
109+
MyService1 service1 = new MyService1();
110+
MyService2 service2 = new MyService2();
111+
112+
MessageHandler handler = MethodMatcherMessageHandler.builder()
113+
.wrap(service1)
114+
// redirects '[]' (empty list) to method MyService1.handler1
115+
.list(it -> it.handler1())
116+
// redirects tuple {any_number, any_string, atom(false)}
117+
// to MyService1.handler2
118+
.tuple(it -> it.handler2(anyInt(), anyString(), eq(false)))
119+
.wrap(service2)
120+
// redirects {42} to MyService2.popa
121+
.tuple(it -> it.popa(42))
122+
.build();
123+
124+
// start it in a separate thread:
125+
handler.startExecutor();
126+
// ...
127+
handler.close();
128+
```

encon-handler/pom.xml

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Copyright 2018 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<parent>
26+
<groupId>io.appulse.encon</groupId>
27+
<artifactId>encon-parent</artifactId>
28+
<version>1.4.0</version>
29+
</parent>
30+
31+
<artifactId>encon-handler</artifactId>
32+
<packaging>jar</packaging>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.projectlombok</groupId>
37+
<artifactId>lombok</artifactId>
38+
<scope>provided</scope>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>${project.groupId}</groupId>
43+
<artifactId>encon</artifactId>
44+
<version>${project.version}</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>${project.groupId}</groupId>
48+
<artifactId>encon-databind</artifactId>
49+
<version>${project.version}</version>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>cglib</groupId>
54+
<artifactId>cglib</artifactId>
55+
<version>3.2.7</version>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>com.google.code.findbugs</groupId>
60+
<artifactId>annotations</artifactId>
61+
<scope>provided</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.google.code.findbugs</groupId>
65+
<artifactId>jsr305</artifactId>
66+
<scope>provided</scope>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>junit</groupId>
71+
<artifactId>junit</artifactId>
72+
<scope>test</scope>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.assertj</groupId>
76+
<artifactId>assertj-core</artifactId>
77+
<scope>test</scope>
78+
</dependency>
79+
</dependencies>
80+
81+
<build>
82+
<plugins>
83+
<plugin>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<artifactId>maven-source-plugin</artifactId>
86+
</plugin>
87+
<plugin>
88+
<groupId>org.apache.maven.plugins</groupId>
89+
<artifactId>maven-javadoc-plugin</artifactId>
90+
</plugin>
91+
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-surefire-plugin</artifactId>
95+
</plugin>
96+
<plugin>
97+
<groupId>org.apache.maven.plugins</groupId>
98+
<artifactId>maven-failsafe-plugin</artifactId>
99+
</plugin>
100+
101+
<plugin>
102+
<groupId>org.codehaus.mojo</groupId>
103+
<artifactId>findbugs-maven-plugin</artifactId>
104+
</plugin>
105+
<plugin>
106+
<groupId>org.apache.maven.plugins</groupId>
107+
<artifactId>maven-pmd-plugin</artifactId>
108+
</plugin>
109+
110+
<plugin>
111+
<groupId>org.apache.maven.plugins</groupId>
112+
<artifactId>maven-checkstyle-plugin</artifactId>
113+
</plugin>
114+
</plugins>
115+
</build>
116+
</project>

0 commit comments

Comments
 (0)