Skip to content

Commit

Permalink
Simplify examples, extend readme (crossbario#331)
Browse files Browse the repository at this point in the history
* Simplify example code; make it precise

* update readme examples
  • Loading branch information
om26er authored Oct 16, 2017
1 parent aeb39f9 commit 105fdde
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 132 deletions.
67 changes: 48 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,67 @@ and finally

$ make java # Starts the java (Netty) based demo client that performs WAMP actions

### Show me some code
## Show me some code

The code in demo-gallery contains some examples on how to use the autobahn library, it also contains convenience methods to use. Below is a basic set of code examples showing all 4 WAMP actions.

Subscribe to a topic
### Subscribe to a topic

```java
public void demonstrateSubscribe(Session session, SessionDetails details) {
// Subscribe to topic to receive its events.
CompletableFuture<Subscription> subFuture = session.subscribe(
"com.myapp.hello", this::onEvent);
subFuture.thenAccept(subscription -> {
// We have successfully subscribed.
System.out.println("Subscribed to topic " + subscription.topic);
CompletableFuture<Subscription> subFuture = session.subscribe("com.myapp.hello",
this::onEvent);
subFuture.whenComplete((subscription, throwable) -> {
if (throwable != null) {
// We have successfully subscribed.
System.out.println("Subscribed to topic " + subscription.topic);
} else {
// Something went bad.
throwable.printStackTrace();
}
});
}

private void onEvent(List<Object> args, Map<String, Object> kwargs, EventDetails details) {
System.out.println(String.format("Got event: %s", args.get(0)));
}
```

Publish to a topic
Since we are only accessing `args` in onEvent(), we could simplify it like:
```java
private void onEvent(List<Object> args) {
System.out.println(String.format("Got event: %s", args.get(0)));
}
```
### Publish to a topic

```java
public void demonstratePublish(Session session, SessionDetails details) {
// Publish to a topic.
CompletableFuture<Publication> pubFuture = session.publish(
"com.myapp.hello", "Hello World!");
// Publish to a topic that takes a single arguments
List<Object> args = Arrays.asList("Hello World!", 900, "UNIQUE");
CompletableFuture<Publication> pubFuture = session.publish("com.myapp.hello", args);
pubFuture.thenAccept(publication -> System.out.println("Published successfully"));
// Shows we can separate out exception handling
pubFuture.exceptionally(throwable -> {
throwable.printStackTrace();
return null;
});
}
```
A simpler call would look like:
```java
public void demonstratePublish(Session session, SessionDetails details) {
CompletableFuture<Publication> pubFuture = session.publish("com.myapp.hello", "Hi!");
...
}
```

Register a procedure
### Register a procedure

```java
public void demonstrateRegister(Session session, SessionDetails details) {
// Register a procedure.
CompletableFuture<Registration> regFuture = session.register(
"com.myapp.add2", this::add2);
CompletableFuture<Registration> regFuture = session.register("com.myapp.add2", this::add2);
regFuture.thenAccept(registration ->
System.out.println("Successfully registered procedure: " + registration.procedure));
}
Expand All @@ -87,20 +108,26 @@ private CompletableFuture<InvocationResult> add2(
return CompletableFuture.completedFuture(new InvocationResult(arr));
}
```
A very precise `add2` may look like:
```java
private List<Object> add2(List<Integer> args, InvocationDetails details) {
int res = args.get(0) + args.get(1);
return Arrays.asList(res, details.session.getID(), "Java");
}
```

Call a procedure
### Call a procedure

```java
public void demonstrateCall(Session session, SessionDetails details) {
// Call a remote procedure.
CompletableFuture<CallResult> callFuture = session.call(
"com.myapp.add2", 10, 20);
CompletableFuture<CallResult> callFuture = session.call("com.myapp.add2", 10, 20);
callFuture.thenAccept(callResult ->
System.out.println(String.format("Call result: %s", callResult.results.get(0))));
}
```

Connecting the dots
### Connecting the dots

```java
public void main() {
Expand All @@ -109,6 +136,8 @@ public void main() {
// Add all onJoin listeners
session.addOnJoinListener(this::demonstrateSubscribe);
session.addOnJoinListener(this::demonstratePublish);
session.addOnJoinListener(this::demonstrateCall);
session.addOnJoinListener(this::demonstrateRegister);

// finally, provide everything to a Client and connect
Client client = new Client(session, url, realm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

package io.crossbar.autobahn.demogallery;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -95,27 +95,21 @@ private void onJoinCallback(Session session, SessionDetails details) {
}, 0, 2, TimeUnit.SECONDS);
}

private void onLeaveCallback(Session session, CloseDetails closeDetails) {
LOGGER.info(String.format("Left reason=%s, message=%s",
closeDetails.reason, closeDetails.message));
private void onLeaveCallback(Session session, CloseDetails detail) {
LOGGER.info(String.format("Left reason=%s, message=%s", detail.reason, detail.message));
}

private void onDisconnectCallback(Session session, boolean wasClean) {
LOGGER.info(String.format("Session with ID=%s, disconnected.", session.getID()));
}

private List<Object> add2(List<Object> args, InvocationDetails details) {
int res = (int) args.get(0) + (int) args.get(1);
List<Object> arr = new ArrayList<>();
arr.add(res);
arr.add(details.session.getID());
arr.add("Java");
return arr;
private List<Object> add2(List<Integer> args, InvocationDetails details) {
int res = args.get(0) + args.get(1);
return Arrays.asList(res, details.session.getID(), "Java");
}

private void onCounter(List<Object> args) {
LOGGER.info(String.format(
"oncounter event, counter value=%s from component %s (%s)",
LOGGER.info(String.format("oncounter event, counter value=%s from component %s (%s)",
args.get(0), args.get(1), args.get(2)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,120 +61,91 @@ public int start(String url, String realm) {
}
}

public void onJoinHandler4(Session session, SessionDetails details) {
private void onJoinHandler4(Session session, SessionDetails details) {
LOGGER.info("onJoinHandler4 fired");

// call a remote procedure that returns a Person
CompletableFuture<Void> f1 =
mSession.call("com.example.get_person", Person.class)
.handleAsync(
(person, throwable) -> {
if (throwable != null) {
LOGGER.info(String.format("get_person() ERROR: %s", throwable.getMessage()));
} else {
LOGGER.info(String.format(
"get_person() [typed]: %s %s (%s)",
person.firstname, person.lastname, person.department));
}
return null;
}, mExecutor
)
;
CompletableFuture<Person> f1 = mSession.call("com.example.get_person", Person.class);
f1.whenCompleteAsync((person, throwable) -> {
if (throwable != null) {
LOGGER.info(String.format("get_person() ERROR: %s", throwable.getMessage()));
} else {
LOGGER.info(String.format("get_person() [typed]: %s %s (%s)", person.firstname,
person.lastname, person.department));
}
}, mExecutor);

// call a remote procedure that returns a Person .. slowly (3 secs delay)
CompletableFuture<Void> f2 =
mSession.call("com.example.get_person_delayed", Person.class)
.handleAsync(
(person, throwable) -> {
if (throwable != null) {
LOGGER.info(String.format("get_person_delayed() ERROR: %s",
throwable.getMessage()));
} else {
LOGGER.info(String.format(
"get_person_delayed() [typed]: %s %s (%s)",
person.firstname, person.lastname, person.department));
}
return null;
}, mExecutor
)
;
CompletableFuture<Person> f2 = mSession.call("com.example.get_person_delayed",
Person.class);
f2.whenCompleteAsync((person, throwable) -> {
if (throwable != null) {
LOGGER.info(String.format("get_person_delayed() ERROR: %s",
throwable.getMessage()));
} else {
LOGGER.info(String.format("get_person_delayed() [typed]: %s %s (%s)",
person.firstname, person.lastname, person.department));
}
}, mExecutor);

// call a remote procedure that returns a List<Person>
CompletableFuture<Void> f3 =
mSession.call("com.example.get_all_persons", new TypeReference<List<Person>>() {})
.handleAsync(
(persons, throwable) -> {
if (throwable != null) {
LOGGER.info(String.format(
"get_all_persons() ERROR: %s", throwable.getMessage()));
} else {
LOGGER.info("get_all_persons() [typed]:");
for (Person person: persons) {
LOGGER.info(String.format("%s %s (%s)",
person.firstname, person.lastname, person.department));
}
}
return null;
}, mExecutor
)
;
CompletableFuture<List<Person>> f3 = mSession.call("com.example.get_all_persons",
new TypeReference<List<Person>>() {});
f3.whenCompleteAsync((persons, throwable) -> {
if (throwable != null) {
LOGGER.info(String.format("get_all_persons() ERROR: %s", throwable.getMessage()));
} else {
LOGGER.info("get_all_persons() [typed]:");
for (Person person: persons) {
LOGGER.info(String.format("%s %s (%s)", person.firstname, person.lastname,
person.department));
}
}
}, mExecutor);

// call a remote procedure that returns a List<Person>
List<Object> args = new ArrayList<>();
args.add("development");

CompletableFuture<Void> f4 =
mSession.call("com.example.get_persons_by_department", args, new TypeReference<List<Person>>() {})
.handleAsync(
(persons, throwable) -> {
if (throwable != null) {
LOGGER.info(String.format(
"get_persons_by_department() ERROR: %s",
throwable.getMessage()));
} else {
LOGGER.info("get_persons_by_department() [typed]:");
for (Person person: persons) {
LOGGER.info(String.format("%s %s (%s)",
person.firstname, person.lastname, person.department));
}
}
return null;
}, mExecutor
)
;
CompletableFuture<List<Person>> f4 = mSession.call("com.example.get_persons_by_department",
args, new TypeReference<List<Person>>() {});
f4.whenCompleteAsync((persons, throwable) -> {
if (throwable != null) {
LOGGER.info(String.format("get_persons_by_department() ERROR: %s",
throwable.getMessage()));
} else {
LOGGER.info("get_persons_by_department() [typed]:");
for (Person person: persons) {
LOGGER.info(String.format("%s %s (%s)", person.firstname, person.lastname,
person.department));
}
}
}, mExecutor);

// call a remote procedure that returns a Map<String, List<Person>>
CompletableFuture<Void> f5 =
mSession.call("com.example.get_persons_by_department", new TypeReference<Map<String, List<Person>>>() {})
.handleAsync(
(Map<String, List<Person>> persons_by_department, Throwable throwable) -> {
if (throwable != null) {
LOGGER.info(String.format(
"get_persons_by_department() ERROR: %s", throwable.getMessage()));
} else {

LOGGER.info("get_persons_by_department() [typed]:");
for (String department: persons_by_department.keySet()) {
LOGGER.info(String.format("department '%s:'", department));
List<Person> persons = persons_by_department.get(department);
for (Person person: persons) {
LOGGER.info(String.format("%s %s", person.firstname, person.lastname));
}
}
}
return null;
}, mExecutor
)
;

CompletableFuture.allOf(f1, f2, f3, f4, f5)
.thenRunAsync(
() -> {
LOGGER.info("all done!");
mSession.leave("wamp.close.normal", "all done!");
}, mExecutor
)
;
CompletableFuture<Map<String, List<Person>>> f5 = mSession.call(
"com.example.get_persons_by_department",
new TypeReference<Map<String, List<Person>>>() {});
f5.whenCompleteAsync((persons_by_department, throwable) -> {
if (throwable != null) {
LOGGER.info(String.format("get_persons_by_department() ERROR: %s",
throwable.getMessage()));
} else {
LOGGER.info("get_persons_by_department() [typed]:");
for (String department: persons_by_department.keySet()) {
LOGGER.info(String.format("department '%s:'", department));
List<Person> persons = persons_by_department.get(department);
for (Person person: persons) {
LOGGER.info(String.format("%s %s", person.firstname, person.lastname));
}
}
}
});

CompletableFuture.allOf(f1, f2, f3, f4, f5).thenRunAsync(() -> {
LOGGER.info("all done!");
mSession.leave("wamp.close.normal", "all done!");
}, mExecutor);
}

static class Person {
Expand Down

0 comments on commit 105fdde

Please sign in to comment.