Skip to content
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

Add ChannelFutures utility class to pulsar-common. #8137

Merged
merged 1 commit into from
Oct 1, 2020

Conversation

racorn
Copy link
Contributor

@racorn racorn commented Sep 25, 2020

Motivation

Pulsar code uses CompletableFuture to implement asynchronous execution paths. When interacting with Netty, a common pattern is to create a CompletableFuture and some glue code to listen for Netty ChannelFuture completions and then complete the CompletableFuture. Pulsar code would be more ease to write, read, and maintain if we add a static utility method for adapting a io.netty.channel.ChannelFuture to a java.util.concurrent.CompletableFuture.

Example:

public CompletableFuture<Client> connect() {
  CompletableFuture<Client> clientFuture = new CompletableFuture<>();
  bootstrap.connect().addListener((ChannelFuture cf) -> {
    if (cf.isSuccess()) {
      client.complete(new Client(cf.channel()));
    } else {
      client.completeExceptionally(cf.cause());
    }
  });
  return clientFuture;
}

can be written as:

public CompletableFuture<Client> connect() {
  return ChannelFutures.toCompletableFuture(bootstrap.connect()).thenApply(channel -> new Client(channel));
}

Modifications

  • Added org.apache.pulsar.common.util.netty.ChannelFutures

Verifying this change

  • Added test class ChannelFuturesTest

Documentation

  • JavaDocs

@racorn
Copy link
Contributor Author

racorn commented Sep 25, 2020

@rdhabalia can you please take a look? I will use this class to improve code committed in #8117 :-)

@racorn
Copy link
Contributor Author

racorn commented Sep 25, 2020

/pulsarbot run-failure-checks

3 similar comments
@racorn
Copy link
Contributor Author

racorn commented Sep 25, 2020

/pulsarbot run-failure-checks

@racorn
Copy link
Contributor Author

racorn commented Sep 25, 2020

/pulsarbot run-failure-checks

@racorn
Copy link
Contributor Author

racorn commented Sep 28, 2020

/pulsarbot run-failure-checks

@sijie sijie added the type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages label Oct 1, 2020
@sijie sijie added this to the 2.7.0 milestone Oct 1, 2020
@sijie sijie merged commit e2d5c05 into apache:master Oct 1, 2020
@racorn racorn deleted the util-channelfutures branch October 1, 2020 08:41
lbenc135 pushed a commit to lbenc135/pulsar that referenced this pull request Oct 3, 2020
### Motivation

Pulsar code uses `CompletableFuture` to implement asynchronous execution paths. When interacting with Netty, a common pattern is to create a `CompletableFuture` and some glue code to listen for Netty `ChannelFuture` completions and then complete the `CompletableFuture`. Pulsar code would be more ease to write, read, and maintain if we add a static utility method for adapting a `io.netty.channel.ChannelFuture` to a `java.util.concurrent.CompletableFuture`.

Example:

```java
public CompletableFuture<Client> connect() {
  CompletableFuture<Client> clientFuture = new CompletableFuture<>();
  bootstrap.connect().addListener((ChannelFuture cf) -> {
    if (cf.isSuccess()) {
      client.complete(new Client(cf.channel()));
    } else {
      client.completeExceptionally(cf.cause());
    }
  });
  return clientFuture;
}
```
can be written as:

```java
public CompletableFuture<Client> connect() {
  return ChannelFutures.toCompletableFuture(bootstrap.connect()).thenApply(channel -> new Client(channel));
}
```

### Modifications

- Added `org.apache.pulsar.common.util.netty.ChannelFutures`

### Verifying this change

- Added test class `ChannelFuturesTest`

### Documentation

  - JavaDocs
@wolfstudy
Copy link
Member

Move this change to 2.6.2, because the #8177 depends on it.

wolfstudy pushed a commit that referenced this pull request Oct 30, 2020
### Motivation

Pulsar code uses `CompletableFuture` to implement asynchronous execution paths. When interacting with Netty, a common pattern is to create a `CompletableFuture` and some glue code to listen for Netty `ChannelFuture` completions and then complete the `CompletableFuture`. Pulsar code would be more ease to write, read, and maintain if we add a static utility method for adapting a `io.netty.channel.ChannelFuture` to a `java.util.concurrent.CompletableFuture`.

Example:

```java
public CompletableFuture<Client> connect() {
  CompletableFuture<Client> clientFuture = new CompletableFuture<>();
  bootstrap.connect().addListener((ChannelFuture cf) -> {
    if (cf.isSuccess()) {
      client.complete(new Client(cf.channel()));
    } else {
      client.completeExceptionally(cf.cause());
    }
  });
  return clientFuture;
}
```
can be written as:

```java
public CompletableFuture<Client> connect() {
  return ChannelFutures.toCompletableFuture(bootstrap.connect()).thenApply(channel -> new Client(channel));
}
```

### Modifications

- Added `org.apache.pulsar.common.util.netty.ChannelFutures`

### Verifying this change

- Added test class `ChannelFuturesTest`

### Documentation

  - JavaDocs

(cherry picked from commit e2d5c05)
huangdx0726 pushed a commit to huangdx0726/pulsar that referenced this pull request Nov 13, 2020
### Motivation

Pulsar code uses `CompletableFuture` to implement asynchronous execution paths. When interacting with Netty, a common pattern is to create a `CompletableFuture` and some glue code to listen for Netty `ChannelFuture` completions and then complete the `CompletableFuture`. Pulsar code would be more ease to write, read, and maintain if we add a static utility method for adapting a `io.netty.channel.ChannelFuture` to a `java.util.concurrent.CompletableFuture`.

Example:

```java
public CompletableFuture<Client> connect() {
  CompletableFuture<Client> clientFuture = new CompletableFuture<>();
  bootstrap.connect().addListener((ChannelFuture cf) -> {
    if (cf.isSuccess()) {
      client.complete(new Client(cf.channel()));
    } else {
      client.completeExceptionally(cf.cause());
    }
  });
  return clientFuture;
}
```
can be written as:

```java
public CompletableFuture<Client> connect() {
  return ChannelFutures.toCompletableFuture(bootstrap.connect()).thenApply(channel -> new Client(channel));
}
```

### Modifications

- Added `org.apache.pulsar.common.util.netty.ChannelFutures`

### Verifying this change

- Added test class `ChannelFuturesTest`

### Documentation

  - JavaDocs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release/2.6.2 type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants