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
This commit is contained in:
Luca Milanesio 2015-02-09 18:46:09 +00:00
parent 781c9f7d83
commit 592d59ceb2
3 changed files with 31 additions and 5 deletions

View File

@ -43,6 +43,7 @@ public class ScalaClientCodegen extends DefaultCodegen implements CodegenConfig
additionalProperties.put("groupId", groupId); additionalProperties.put("groupId", groupId);
additionalProperties.put("artifactId", artifactId); additionalProperties.put("artifactId", artifactId);
additionalProperties.put("artifactVersion", artifactVersion); additionalProperties.put("artifactVersion", artifactVersion);
additionalProperties.put("asyncHttpClient", false);
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
supportingFiles.add(new SupportingFile("apiInvoker.mustache", supportingFiles.add(new SupportingFile("apiInvoker.mustache",

View File

@ -35,10 +35,12 @@ object ScalaJsonUtil {
} }
} }
object ApiInvoker { class ApiInvoker(val mapper: ObjectMapper = ScalaJsonUtil.getJsonMapper,
val mapper = ScalaJsonUtil.getJsonMapper httpHeaders: HashMap[String, String] = HashMap(),
val defaultHeaders: HashMap[String, String] = HashMap() hostMap: HashMap[String, Client] = HashMap(),
val hostMap: HashMap[String, Client] = HashMap() asyncHttpClient: Boolean = false) {
var defaultHeaders: HashMap[String, String] = httpHeaders
def escape(value: String): String = { def escape(value: String): String = {
URLEncoder.encode(value, "utf-8").replaceAll("\\+", "%20") URLEncoder.encode(value, "utf-8").replaceAll("\\+", "%20")
@ -151,14 +153,30 @@ object ApiInvoker {
hostMap.contains(host) match { hostMap.contains(host) match {
case true => hostMap(host) case true => hostMap(host)
case false => { case false => {
val client = Client.create() val client = newClient(host)
// client.addFilter(new LoggingFilter()) // client.addFilter(new LoggingFilter())
hostMap += host -> client hostMap += host -> client
client client
} }
} }
} }
def newClient(host: String): Client = asyncHttpClient match {
case true => {
import org.sonatype.spice.jersey.client.ahc.config.DefaultAhcConfig
import org.sonatype.spice.jersey.client.ahc.AhcHttpClient
val config: DefaultAhcConfig = new DefaultAhcConfig()
AhcHttpClient.create(config)
} }
case _ => Client.create()
}
}
object ApiInvoker extends ApiInvoker(mapper = ScalaJsonUtil.getJsonMapper,
httpHeaders = HashMap(),
hostMap = HashMap(),
asyncHttpClient = {{asyncHttpClient}})
class ApiException(val code: Int, msg: String) extends RuntimeException(msg) class ApiException(val code: Int, msg: String) extends RuntimeException(msg)

View File

@ -165,6 +165,12 @@
<artifactId>jersey-multipart</artifactId> <artifactId>jersey-multipart</artifactId>
<version>${jersey-version}</version> <version>${jersey-version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.jfarcand</groupId>
<artifactId>jersey-ahc-client</artifactId>
<version>${jersey-async-version}</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>org.scala-lang</groupId> <groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId> <artifactId>scala-library</artifactId>
@ -204,6 +210,7 @@
<joda-time-version>2.2</joda-time-version> <joda-time-version>2.2</joda-time-version>
<jersey-version>1.7</jersey-version> <jersey-version>1.7</jersey-version>
<swagger-core-version>1.5.0-M1</swagger-core-version> <swagger-core-version>1.5.0-M1</swagger-core-version>
<jersey-async-version>1.0.5</jersey-async-version>
<maven-plugin.version>1.0.0</maven-plugin.version> <maven-plugin.version>1.0.0</maven-plugin.version>
<jackson-version>2.4.2</jackson-version> <jackson-version>2.4.2</jackson-version>