Skip to content

Commit 2386e39

Browse files
committed
Support for HTTP Async client transport for Jersey on Scala
When generating Scala client, the ApiInvoker is a class with overridable options, avoiding the need of public vars to be manually modified. Additionally it is possible to plug the AsyncHttp protocol for Jersey in order to achieve Netty support and maximum scalability. NOTE: Support for Scala 2.10.0-3 removed as largely obsolete
1 parent f55d9ea commit 2386e39

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ javacOptions ++= Seq("-target", "1.6", "-source", "1.6", "-Xlint:unchecked", "-X
1313

1414
scalacOptions ++= Seq("-optimize", "-unchecked", "-deprecation", "-Xcheckinit", "-encoding", "utf8")
1515

16-
crossScalaVersions := Seq("2.10.0", "2.10.1", "2.10.2", "2.10.3", "2.10.4", "2.11.0", "2.11.1")
16+
crossScalaVersions := Seq("2.10.4", "2.11.0", "2.11.1")
1717

1818
scalaVersion := "2.10.4"
1919

sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ declare -r default_jvm_opts="-Dfile.encoding=UTF8 -XX:MaxPermSize=256m -Xms512m
128128
declare -r noshare_opts="-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy"
129129
declare -r latest_28="2.8.2"
130130
declare -r latest_29="2.9.3"
131-
declare -r latest_210="2.10.0"
131+
declare -r latest_210="2.10.4"
132132

133133
declare -r script_path=$(get_script_path "$BASH_SOURCE")
134134
declare -r script_dir="$(dirname $script_path)"

src/main/resources/scala/apiInvoker.mustache

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ object ScalaJsonUtil {
3535
}
3636
}
3737

38-
object ApiInvoker {
39-
val mapper = ScalaJsonUtil.getJsonMapper
40-
val defaultHeaders: HashMap[String, String] = HashMap()
41-
val hostMap: HashMap[String, Client] = HashMap()
38+
class ApiInvoker(val mapper: ObjectMapper = ScalaJsonUtil.getJsonMapper,
39+
httpHeaders: HashMap[String, String] = HashMap(),
40+
hostMap: HashMap[String, Client] = HashMap(),
41+
asyncHttpClient: Boolean = false) {
42+
43+
var defaultHeaders: HashMap[String, String] = httpHeaders
4244
4345
def escape(value: String): String = {
4446
URLEncoder.encode(value, "utf-8").replaceAll("\\+", "%20")
@@ -151,14 +153,30 @@ object ApiInvoker {
151153
hostMap.contains(host) match {
152154
case true => hostMap(host)
153155
case false => {
154-
val client = Client.create()
156+
val client = newClient(host)
155157
// client.addFilter(new LoggingFilter())
156158
hostMap += host -> client
157159
client
158160
}
159161
}
160162
}
163+
164+
def newClient(host: String): Client = asyncHttpClient match {
165+
case true => {
166+
import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig
167+
import org.sonatype.spice.jersey.client.ahc.AhcHttpClient
168+
169+
val config: DefaultAhcConfig = new DefaultAhcConfig()
170+
AhcHttpClient.create(config)
171+
}
172+
case _ => Client.create()
173+
}
161174
}
162175

176+
object ApiInvoker extends ApiInvoker(mapper = ScalaJsonUtil.getJsonMapper,
177+
httpHeaders = HashMap(),
178+
hostMap = HashMap(),
179+
asyncHttpClient = {{asyncHttpClient}})
180+
163181
class ApiException(val code: Int, msg: String) extends RuntimeException(msg)
164182

src/main/resources/scala/pom.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@
162162
<version>${jersey-version}</version>
163163
<scope>compile</scope>
164164
</dependency>
165+
<dependency>
166+
<groupId>org.jfarcand</groupId>
167+
<artifactId>jersey-ahc-client</artifactId>
168+
<version>${jersey-async-version}</version>
169+
<scope>compile</scope>
170+
</dependency>
165171
<dependency>
166172
<groupId>org.scala-lang</groupId>
167173
<artifactId>scala-library</artifactId>
@@ -221,6 +227,7 @@
221227
</profiles>
222228
<properties>
223229
<jersey-version>1.7</jersey-version>
230+
<jersey-async-version>1.0.5</jersey-async-version>
224231
<junit-version>4.8.1</junit-version>
225232
<maven-plugin.version>1.0.0</maven-plugin.version>
226233
<junit-version>4.8.1</junit-version>

src/main/scala/com/wordnik/swagger/codegen/BasicScalaGenerator.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ class BasicScalaGenerator extends BasicGenerator {
212212
additionalParams ++= Map(
213213
"artifactId" -> "scala-client",
214214
"artifactVersion" -> "1.0.0",
215-
"groupId" -> "com.wordnik")
215+
"groupId" -> "com.wordnik",
216+
"asyncHttpClient" -> "false")
216217

217218
// supporting classes
218219
override def supportingFiles = List(

0 commit comments

Comments
 (0)