Skip to content

Simplify mailbox #3

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 2 commits into from
Jun 25, 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
16 changes: 13 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,26 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

- Refactoring external and internal APIs.
- Speed-up serialization/deserialization.
- Provide more custom exceptions.
- Review debug logs.
- Add `monitor` functionality.
- Add tests.
- Split `protocol`, `module` and `config` on separate sub-projects.
- Rething caches in terms ans modules.
- Rething caches in terms and modules.
- Add lazy deserialization for terms.

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

Refactoring and cleaning.

### Added

- `bstring` methods to `Erlang` term helper class.

### Changed

- Refactored external and internal APIs.

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

Small refactoring and cleaning.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ We use [SemVer](http://semver.org/) for versioning. For the versions available,

* **[Artem Labazin](https://github.com/xxlabaza)** - creator and the main developer

* **[Sokol Andrey](https://github.com/SokolAndrey)** - texts corrector and mastermind

## License

This project is licensed under the Apache License 2.0 License - see the [license](./LICENSE) file for details
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.1.0</version>
<version>1.2.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.1.0</version>
<version>1.2.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.1.0'
compile 'io.appulse.encon.java:encon-config:1.2.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.1.0</version>
<version>1.2.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.1.0</version>
<version>1.2.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.1.0'
compile 'io.appulse.encon.java:encon-databind:1.2.0'
```

Let's imagine, you have POJO like this:
Expand Down
8 changes: 1 addition & 7 deletions 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.1.0</version>
<version>1.2.0</version>
</parent>

<artifactId>encon-databind</artifactId>
Expand Down Expand Up @@ -53,12 +53,6 @@ limitations under the License.
<artifactId>utils-java</artifactId>
</dependency>

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions encon-terms/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.1.0</version>
<version>1.2.0</version>
</parent>

<artifactId>encon-terms</artifactId>
Expand Down Expand Up @@ -57,7 +57,7 @@ limitations under the License.

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<artifactId>netty-buffer</artifactId>
<scope>provided</scope>
</dependency>

Expand Down
45 changes: 45 additions & 0 deletions encon-terms/src/main/java/io/appulse/encon/terms/Erlang.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -211,6 +212,50 @@ public static ErlangString string (@NonNull String value) {
return new ErlangString(value);
}

/**
* Creates new {@link ErlangBinary} from string with default charset.
*
* @param value represent string
*
* @return {@link ErlangBinary} new instance
*
* @since 1.20
*/
public static ErlangBinary bstring (String value) {
return bstring(value, Charset.defaultCharset());
}

/**
* Creates new {@link ErlangBinary} from string with given charset.
*
* @param value represent string
*
* @param charset charset
*
* @return {@link ErlangBinary} new instance
*
* @since 1.20
*/
public static ErlangBinary bstring (String value, @NonNull String charset) {
return bstring(value, Charset.forName(charset));
}

/**
* Creates new {@link ErlangBinary} from string with given charset.
*
* @param value represent string
*
* @param charset charset
*
* @return {@link ErlangBinary} new instance
*
* @since 1.20
*/
public static ErlangBinary bstring (@NonNull String value, @NonNull Charset charset) {
byte[] bytes = value.getBytes(charset);
return binary(bytes);
}

/**
* Creates new {@link ErlangTuple} instance.
*
Expand Down
103 changes: 78 additions & 25 deletions encon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ First of all, add encon's dependency:
<dependency>
<groupId>io.appulse.encon</groupId>
<artifactId>encon</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
</dependency>
...
</dependencies>
Expand All @@ -23,22 +23,16 @@ First of all, add encon's dependency:
**Gradle**:

```groovy
compile 'io.appulse.encon.java:encon:1.1.0'
compile 'io.appulse.encon.java:encon:1.2.0'
```

Let's write a simple Erlang's `echo`-server:
Let's create a new `Erlang` node:

```java

import static io.appulse.encon.java.terms.Erlang.tuple;

import io.appulse.encon.java.config.NodeConfig;
import io.appulse.encon.java.Node;
import io.appulse.encon.java.Nodes;
import io.appulse.encon.module.connection.regular.Message;
import io.appulse.encon.module.mailbox.Mailbox;
import io.appulse.encon.terms.ErlangTerm;



// Creating node's config.
Expand All @@ -47,36 +41,95 @@ NodeConfig config = NodeConfig.builder()
.cookie("secret")
.build();


// Creates, registers and starts a new Erlang node
Node node = Nodes.singleNode("echo-node", config);

```

After `node` creation, we could register several mailboxes:

> **IMPORTANT:** The main differenece between `MailboxQueueType.NON_BLOCKING` and `MailboxQueueType.BLOCKING` mailbox types is the way to extract the values from a queue.
>
> * `MailboxQueueType.NON_BLOCKING` - uses java.util.Queue.poll() under the hood;
>
> * `MailboxQueueType.BLOCKING` - uses java.util.concurrent.BlockingQueue.take() method (automatically performs type checking)

```java

import static io.appulse.encon.mailbox.MailboxQueueType.NON_BLOCKING;
import static io.appulse.encon.java.terms.Erlang.tuple;

import io.appulse.encon.connection.regular.Message;
import io.appulse.encon.mailbox.Mailbox;
import io.appulse.encon.terms.ErlangTerm;


// Handling option #1
// You can handle incoming request via providing special handler.
// Mailbox #1
// ----------
// Default mailbox's type is `MailboxQueueType.BLOCKING`,
// it uses `java.util.concurrent.LinkedBlockingQueue` under the hood.
//
Mailbox mailbox1 = node.mailbox()
.name("echo-mailbox-1")
.handler((self, header, body) -> {
// expects tuple, where the first element (index - 0) is a caller Pid
self.request()
.body(tuple(self.getPid(), body.getUnsafe(1)))
.send(body.getUnsafe(0).asPid());
})
.build();

for (int count = 0; count < 3; count++) {
// Mailbox.receive() is a blocking operation in that case
Message message = mailbox.receive();

ErlangTerm body = message.getBody();
mailbox1.send("another-node", "another-mailbox", tuple(
mailbox1.getPid(), body.getUnsafe(1)
));
}

// Handling option #2
// You can also use future API and do something like infinity loop.

// Mailbox #2
// ----------
// Specify `MailboxQueueType.NON_BLOCKING` type, in this case
// it uses `java.util.concurrent.ConcurrentLinkedQueue` under the hood.
//
Mailbox mailbox2 = node.mailbox()
.name("echo-mailbox-2")
.type(NON_BLOCKING)
.build();

while (true) {
// 'receive' - is a blocking operation
// Mailbox.receive() is a non-blocking operation in this case,
// that is why we have `if` clause.
Message message = mailbox2.receive();
ErlangTerm body = message.getBodyUnsafe();
mailbox2.request()
.body(tuple(mailbox2.getPid(), body.getUnsafe(1)))
.send(body.getUnsafe(0).asPid());
if (message == null) {
continue;
}
ErlangTerm body = message.getBody();
mailbox2.send("another-node", "another-mailbox", tuple(
mailbox2.getPid(), body.getUnsafe(1)
));
}


// Mailbox #3
// ----------
// Specify `MailboxQueueType.NON_BLOCKING` type and
// set our own `java.util.Queue` instance.
//
Mailbox mailbox3 = node.mailbox()
.name("echo-mailbox-2")
.type(NON_BLOCKING)
.queue(new LinkedBlockingQueue<>())
.build();

while (true) {
// Mailbox.receive() is a non-blocking operation in this case,
// that is why we have `if` clause.
Message message = mailbox3.receive();
if (message == null) {
continue;
}
ErlangTerm body = message.getBody();
mailbox3.send("another-node", "another-mailbox", tuple(
mailbox3.getPid(), body.getUnsafe(1)
));
}

```
12 changes: 9 additions & 3 deletions encon/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.1.0</version>
<version>1.2.0</version>
</parent>

<artifactId>encon</artifactId>
Expand Down Expand Up @@ -83,9 +83,15 @@ limitations under the License.
<artifactId>snakeyaml</artifactId>
<version>1.21</version>
</dependency>

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<artifactId>netty-handler</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<classifier>linux-x86_64</classifier>
</dependency>

<dependency>
Expand Down Expand Up @@ -130,7 +136,7 @@ limitations under the License.
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>

<plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
</plugin>
Expand Down
Loading