Skip to content

Commit fbecc20

Browse files
authored
Merge pull request #407 from HubSpot/js-horizon-0-5-0
upgrade horizon to 0.5.0, netty from 3 to 4
2 parents af008d3 + 2a19128 commit fbecc20

File tree

8 files changed

+100
-76
lines changed

8 files changed

+100
-76
lines changed

pom.xml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<parent>
66
<groupId>com.hubspot</groupId>
77
<artifactId>basepom</artifactId>
8-
<version>63.8</version>
8+
<version>65.1</version>
99
</parent>
1010

1111
<groupId>com.hubspot.slack</groupId>
1212
<artifactId>slack-client</artifactId>
13-
<version>1.16.19-SNAPSHOT</version>
13+
<version>1.17.0-SNAPSHOT</version>
1414
<packaging>pom</packaging>
1515

1616
<name>${project.groupId}:${project.artifactId}</name>
@@ -25,9 +25,7 @@
2525
</modules>
2626

2727
<properties>
28-
<horizon.version>0.1.1</horizon.version>
29-
<dep.netty3.version>3.10.6.Final</dep.netty3.version>
30-
<dep.plugin.spotbugs.version>4.9.3.2</dep.plugin.spotbugs.version>
28+
<horizon.version>0.5.0</horizon.version>
3129
</properties>
3230

3331
<dependencyManagement>
@@ -82,12 +80,6 @@
8280
<artifactId>slack-request-verifier</artifactId>
8381
<version>${project.version}</version>
8482
</dependency>
85-
86-
<dependency>
87-
<groupId>com.ning</groupId>
88-
<artifactId>async-http-client</artifactId>
89-
<version>1.9.40</version>
90-
</dependency>
9183
</dependencies>
9284
</dependencyManagement>
9385

slack-base/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.hubspot.slack</groupId>
77
<artifactId>slack-client</artifactId>
8-
<version>1.16.19-SNAPSHOT</version>
8+
<version>1.17.0-SNAPSHOT</version>
99
</parent>
1010

1111
<artifactId>slack-base</artifactId>

slack-java-client-examples/pom.xml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.hubspot.slack</groupId>
77
<artifactId>slack-client</artifactId>
8-
<version>1.16.19-SNAPSHOT</version>
8+
<version>1.17.0-SNAPSHOT</version>
99
</parent>
1010

1111
<artifactId>slack-java-client-examples</artifactId>
@@ -29,10 +29,6 @@
2929
<groupId>com.hubspot</groupId>
3030
<artifactId>HorizonCore</artifactId>
3131
</dependency>
32-
<dependency>
33-
<groupId>com.hubspot</groupId>
34-
<artifactId>HorizonNing</artifactId>
35-
</dependency>
3632
<dependency>
3733
<groupId>com.hubspot.slack</groupId>
3834
<artifactId>slack-base</artifactId>
@@ -41,6 +37,22 @@
4137
<groupId>com.hubspot.slack</groupId>
4238
<artifactId>slack-java-client</artifactId>
4339
</dependency>
40+
<dependency>
41+
<groupId>io.netty</groupId>
42+
<artifactId>netty-common</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>io.netty</groupId>
46+
<artifactId>netty-codec-http</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.netty</groupId>
50+
<artifactId>netty-buffer</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>io.netty</groupId>
54+
<artifactId>netty-transport</artifactId>
55+
</dependency>
4456
<dependency>
4557
<groupId>org.slf4j</groupId>
4658
<artifactId>slf4j-api</artifactId>

slack-java-client-examples/src/main/java/com/hubspot/slack/server/example/SlackMessageHandler.java

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
package com.hubspot.slack.server.example;
22

3-
import com.hubspot.horizon.shaded.org.jboss.netty.buffer.ChannelBuffer;
4-
import com.hubspot.horizon.shaded.org.jboss.netty.channel.Channel;
5-
import com.hubspot.horizon.shaded.org.jboss.netty.channel.ChannelHandlerContext;
6-
import com.hubspot.horizon.shaded.org.jboss.netty.channel.ExceptionEvent;
7-
import com.hubspot.horizon.shaded.org.jboss.netty.channel.MessageEvent;
8-
import com.hubspot.horizon.shaded.org.jboss.netty.channel.SimpleChannelHandler;
9-
import com.hubspot.horizon.shaded.org.jboss.netty.handler.codec.http.HttpRequest;
103
import com.hubspot.slack.client.SlackClient;
114
import com.hubspot.slack.client.examples.BasicRuntimeConfig;
125
import com.hubspot.slack.client.jackson.ObjectMapperUtils;
@@ -20,14 +13,19 @@
2013
import com.hubspot.slack.client.models.interaction.SlackInteractiveCallback;
2114
import com.hubspot.slack.client.models.response.views.ModalViewCommandResponse;
2215
import com.hubspot.slack.client.models.views.ModalViewPayload;
16+
import io.netty.buffer.ByteBuf;
17+
import io.netty.channel.ChannelHandlerContext;
18+
import io.netty.channel.SimpleChannelInboundHandler;
19+
import io.netty.handler.codec.http.HttpContent;
20+
import io.netty.handler.codec.http.HttpObject;
2321
import java.io.IOException;
2422
import java.net.URLDecoder;
2523
import java.nio.charset.StandardCharsets;
26-
import java.util.Arrays;
24+
import java.util.Collections;
2725
import org.slf4j.Logger;
2826
import org.slf4j.LoggerFactory;
2927

30-
public class SlackMessageHandler extends SimpleChannelHandler {
28+
public class SlackMessageHandler extends SimpleChannelInboundHandler<HttpObject> {
3129

3230
private static final Logger LOG = LoggerFactory.getLogger(SlackMessageHandler.class);
3331

@@ -38,36 +36,42 @@ public SlackMessageHandler() {
3836
}
3937

4038
@Override
41-
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
42-
HttpRequest request = (HttpRequest) e.getMessage();
43-
ChannelBuffer content = request.getContent();
44-
try {
45-
String jsonContent = URLDecoder
46-
.decode(content.toString(StandardCharsets.UTF_8), "utf-8")
47-
.substring(8);
48-
SlackInteractiveCallback callback = ObjectMapperUtils
49-
.mapper()
50-
.readValue(jsonContent, SlackInteractiveCallback.class);
51-
LOG.info("Received raw JSON: {}", jsonContent);
52-
LOG.info("Deserialized Callback: {}", callback);
53-
if (callback instanceof BlockActions) {
54-
sendResponse((BlockActions) callback);
39+
protected void channelRead0(
40+
ChannelHandlerContext channelHandlerContext,
41+
HttpObject msg
42+
) throws Exception {
43+
if (msg instanceof HttpContent) {
44+
HttpContent httpContent = (HttpContent) msg;
45+
ByteBuf content = httpContent.content();
46+
47+
try {
48+
String jsonContent = URLDecoder
49+
.decode(content.toString(StandardCharsets.UTF_8), StandardCharsets.UTF_8)
50+
.substring(8);
51+
SlackInteractiveCallback callback = ObjectMapperUtils
52+
.mapper()
53+
.readValue(jsonContent, SlackInteractiveCallback.class);
54+
LOG.info("Received raw JSON: {}", jsonContent);
55+
LOG.info("Deserialized Callback: {}", callback);
56+
if (callback instanceof BlockActions) {
57+
sendResponse((BlockActions) callback);
58+
}
59+
} catch (IOException ex) {
60+
LOG.error("Could not decode message", ex);
5561
}
56-
} catch (IOException ex) {
57-
LOG.error("Could not decode message", ex);
5862
}
5963
}
6064

6165
@Override
62-
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
63-
LOG.error("Got an error", e.getCause());
64-
Channel ch = e.getChannel();
65-
ch.close();
66+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
67+
throws Exception {
68+
LOG.error("Got an error", cause.getCause());
69+
ctx.close();
6670
}
6771

6872
private void sendResponse(BlockActions blockActions) {
6973
for (BlockElementAction action : blockActions.getElementActions()) {
70-
LOG.info("You interaced with: {}", action);
74+
LOG.info("You interacted with: {}", action);
7175
}
7276

7377
ModalViewCommandResponse response = slackClient
@@ -76,7 +80,7 @@ private void sendResponse(BlockActions blockActions) {
7680
blockActions.getTriggerId(),
7781
ModalViewPayload.of(
7882
Text.of(TextType.PLAIN_TEXT, "Hi " + blockActions.getUser().getUsername()),
79-
Arrays.asList(
83+
Collections.singletonList(
8084
Section
8185
.of(Text.of(TextType.MARKDOWN, "Thanks for clicking on _something_!"))
8286
.withAccessory(DatePicker.of("my-date-picker"))
Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
11
package com.hubspot.slack.server.example;
22

3-
import com.hubspot.horizon.shaded.org.jboss.netty.bootstrap.ServerBootstrap;
4-
import com.hubspot.horizon.shaded.org.jboss.netty.channel.ChannelFactory;
5-
import com.hubspot.horizon.shaded.org.jboss.netty.channel.Channels;
6-
import com.hubspot.horizon.shaded.org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
7-
import com.hubspot.horizon.shaded.org.jboss.netty.handler.codec.http.HttpChunkAggregator;
8-
import com.hubspot.horizon.shaded.org.jboss.netty.handler.codec.http.HttpServerCodec;
9-
import java.net.InetSocketAddress;
10-
import java.util.concurrent.Executors;
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelInitializer;
6+
import io.netty.channel.ChannelOption;
7+
import io.netty.channel.EventLoopGroup;
8+
import io.netty.channel.nio.NioEventLoopGroup;
9+
import io.netty.channel.socket.SocketChannel;
10+
import io.netty.channel.socket.nio.NioServerSocketChannel;
11+
import io.netty.handler.codec.http.HttpRequestDecoder;
1112

1213
public class SlackServer {
1314

14-
public static void main(String[] args) {
15-
ChannelFactory factory = new NioServerSocketChannelFactory(
16-
Executors.newCachedThreadPool(),
17-
Executors.newCachedThreadPool()
18-
);
15+
public static void main(String[] args) throws Exception {
16+
EventLoopGroup bossGroup = new NioEventLoopGroup();
17+
EventLoopGroup workerGroup = new NioEventLoopGroup();
18+
try {
19+
ServerBootstrap b = new ServerBootstrap();
20+
b
21+
.group(bossGroup, workerGroup)
22+
.channel(NioServerSocketChannel.class)
23+
.option(ChannelOption.SO_BACKLOG, 100)
24+
.localAddress(8080)
25+
.childOption(ChannelOption.TCP_NODELAY, true)
26+
.childOption(ChannelOption.SO_KEEPALIVE, true)
27+
.childHandler(
28+
new ChannelInitializer<SocketChannel>() {
29+
@Override
30+
public void initChannel(SocketChannel ch) throws Exception {
31+
ch.pipeline().addLast(new HttpRequestDecoder(), new SlackMessageHandler());
32+
}
33+
}
34+
);
1935

20-
ServerBootstrap bootstrap = new ServerBootstrap(factory);
36+
// Start the server.
37+
ChannelFuture f = b.bind().sync();
2138

22-
bootstrap.setPipelineFactory(() ->
23-
Channels.pipeline(
24-
new HttpServerCodec(),
25-
new HttpChunkAggregator(2 << 10),
26-
new SlackMessageHandler()
27-
)
28-
);
39+
// Wait until the server socket is closed.
40+
f.channel().closeFuture().sync();
41+
} finally {
42+
// Shut down all event loops to terminate all threads.
43+
bossGroup.shutdownGracefully();
44+
workerGroup.shutdownGracefully();
2945

30-
bootstrap.setOption("child.tcpNoDelay", true);
31-
bootstrap.setOption("child.keepAlive", true);
32-
33-
bootstrap.bind(new InetSocketAddress(8080));
46+
// Wait until all threads are terminated.
47+
bossGroup.terminationFuture().sync();
48+
workerGroup.terminationFuture().sync();
49+
}
3450
}
3551
}

slack-java-client-guice/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.hubspot.slack</groupId>
77
<artifactId>slack-client</artifactId>
8-
<version>1.16.19-SNAPSHOT</version>
8+
<version>1.17.0-SNAPSHOT</version>
99
</parent>
1010

1111
<artifactId>slack-java-client-guice</artifactId>

slack-java-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.hubspot.slack</groupId>
77
<artifactId>slack-client</artifactId>
8-
<version>1.16.19-SNAPSHOT</version>
8+
<version>1.17.0-SNAPSHOT</version>
99
</parent>
1010

1111
<artifactId>slack-java-client</artifactId>

slack-request-verifier/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.hubspot.slack</groupId>
77
<artifactId>slack-client</artifactId>
8-
<version>1.16.19-SNAPSHOT</version>
8+
<version>1.17.0-SNAPSHOT</version>
99
</parent>
1010

1111
<artifactId>slack-request-verifier</artifactId>

0 commit comments

Comments
 (0)