小编Mar*_*ill的帖子

用于java.util.concurrent.Future的scala.concurrent.Future包装器

我正在使用Play Framework 2.1.1和一个生成java.util.concurrent.Future结果的外部Java库.我正在使用scala future而不是Akka,我认为这是Play 2.1的正确选择.如何在保持代码无阻塞的同时将java.util.concurrent.Future包装到scala.concurrent.Future中?

def geConnection() : Connection = {
  // blocking with get
  connectionPool.getConnectionAsync().get(30000, TimeUnit.MILLISECONDS)
}
Run Code Online (Sandbox Code Playgroud)

上面的代码返回一个连接,但使用了get,所以它变成了阻塞

def getConnectionFuture() : Future[Connection] = {
  future {
    // how to remove blocking get and return a scala future?
    connectionPool.getConnectionAsync().get(30000, TimeUnit.MILLISECONDS)
  }
}
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想要一个scala函数,它返回连接作为未来,如上面的代码,但没有通过get阻止代码.我需要在函数中添加什么才能使其无阻塞.

任何指针都会很棒.

java concurrency scala future playframework-2.1

21
推荐指数
2
解决办法
1万
查看次数

结合EitherT和未来

我有一个应用程序,可以对不同的后端系统进行大量调用,并希望使用for-comprehensions来简化后端系统的流程.

我希望结合使用EitherT(scalaz)和Future(scala 2.10),这样我就可以捕获第一个潜在错误(其未来或后端系统问题),并向最终用户返回相应的消息.我已经快速查看了scalaz验证,但是建议捕获第一个错误而不是所有错误都是使用EitherT.

我首先在REPL中尝试一个简单的例子,但是我收到了以下错误

错误:找不到参数F的隐含值:scalaz.Functor [scala.concurrent.Future]

import scala.concurrent._
import scalaz._
import Scalaz._
import ExecutionContext.Implicits.global

type EitherFuture[+A] = EitherT[Future, String, A]

def method1Success : EitherFuture[Int] = {
  println("method 1 success")
  EitherT {
    Future {
      1.right
    }
  }
}

def method2Failure : EitherFuture[Int] = {
  println("method 2 failure")
  EitherT {
    Future {
      "fail".left
    }
  }
}

val m1 = method1Success

// problem
m1.isRight

// problem
def methodChain1 = {
  for {
    a <- method1Success
    b <- method2Failure
  } yield b
}
Run Code Online (Sandbox Code Playgroud)

我仍然是scala和scalaz的新手,所以任何指针都会很棒. …

scala future scalaz for-comprehension scalaz7

17
推荐指数
1
解决办法
5035
查看次数

使用Heroku获取相互SSL身份验证信息

我正在寻找与Heroku建立一个共同的ssl身份验证,其中第三方调用Heroku端点,而Heroku的响应取决于哪个第三方调用Heroku.我需要使用相互的ssl,因为第三方非常注重安全.

我有第三方用证书调用Heroku(通过SSL插件)并且能够得到回复.因此,相互SSL握手似乎有效.

但是,由于没有要检查的证书信息,我的应用程序无法确定哪个第三方调用了Heroku.我查看了Heroku标题,看看是否有SSL添加提供的其他信息,但找不到任何内容.

有没有办法通过Heroku的任何其他方法从相互sal握手获取证书信息?

ssl heroku ssl-certificate mutual-authentication

9
推荐指数
1
解决办法
1473
查看次数

使用tokio_timer重复执行Rust任务

我正在使用Tokio框架在Rust中创建重复任务。下面的代码基于完成的更改请求,以将该功能添加到Tokio-Timer板条箱中。

尝试编译时,出现错误消息:

error[E0281]: type mismatch: the type `fn() {my_cron_func}` implements the trait `std::ops::FnMut<()>`, but the trait `std::ops::FnMut<((),)>` is required (expected tuple, found ())
  --> src/main.rs:19:36
   |
19 |     let background_tasks = wakeups.for_each(my_cron_func);
   |                                    ^^^^^^^^

error[E0281]: type mismatch: the type `fn() {my_cron_func}` implements the trait `std::ops::FnOnce<()>`, but the trait `std::ops::FnOnce<((),)>` is required (expected tuple, found ())
  --> src/main.rs:19:36
   |
19 |     let background_tasks = wakeups.for_each(my_cron_func);
   |                                    ^^^^^^^^

error[E0281]: type mismatch: the type `fn() {my_cron_func}` implements the trait `std::ops::FnMut<()>`, but …
Run Code Online (Sandbox Code Playgroud)

rust rust-tokio

7
推荐指数
1
解决办法
1635
查看次数

使用Anorm使用包含许多列的表进行批量插入

我正在尝试使用Anorm(在播放框架2.3.1中)批量插入MySQL数据库表.我正在构建的应用程序除了需要批量数据插入之外还有一个标准的Web前端,我想尝试将逻辑保留在同一个软件堆栈上.

插入只进入相同的几个表.

一次插入的行数将达到数百,可能会达到数千,我希望由于anorm/mysql /其他限制,我可能需要限制插入行的数量.

我正在使用的MySQL驱动程序是mysql-connector-java - 5.1.31

下面是一个减少用例.

使用表格:

CREATE TABLE table1
(
  col1    INTEGER   NOT NULL,
  col2    BIGINT,
  col3    VARCHAR(255)
); 
Run Code Online (Sandbox Code Playgroud)

和scala代码:

import play.api.Play.current
import play.api.db.DB
import anorm._ 

object TestInserts {

  DB.withConnection("spo") { implicit conn => 

    val theInserts = Seq(
       Seq[NamedParameter]('val1 -> 1, 'val2 -> Some(1L), 'val3 -> Some("One"))
      ,Seq[NamedParameter]('val1 -> 2, 'val2 -> Some(2L), 'val3 -> Some("Two"))
      ,Seq[NamedParameter]('val1 -> 3, 'val2 -> Some(3L), 'val3 -> Some("Three"))
    )

    val insertBatchSQL = BatchSql( SQL("insert into table1 (col1, col2, col3) values …
Run Code Online (Sandbox Code Playgroud)

mysql scala anorm

3
推荐指数
1
解决办法
2773
查看次数

使用scala中的反射进行动态对象方法调用

我正在寻找一种方法来动态调用逻辑,具体取决于scala中的模板ID.因此模板id 1调用逻辑a,模板id 2调用逻辑b等.逻辑将是多样的但具有相同的输入/输出.此外,不同模板ID的数量将达到数千个,并且不会提前知道,因此松耦合感觉要走的路.

我已经开始使用scala 2.11.1来查看反射,并且当我知道提前使用的逻辑但是没有找到动态使用反射的正确方法时可以静态地使用反射,所以例如传入模板id 2将调用逻辑b.

下面是一个简短的示例,显示静态版本的工作原理以及我到目前为止动态版本的骨架.

package thePackage

import scala.reflect.runtime.{universe => ru}

trait theTrait { def theMethod(x: String): Unit }

// the different logic held in different objects
object object1 extends theTrait {
  def theMethod(x: String) = { println("a " + x ) }
}

object object2 extends theTrait { 
  def theMethod(x: String) = { println("b " + x ) }
}

object object3 extends theTrait {
  def theMethod(x: String) = { println("c " + x ) }
} …
Run Code Online (Sandbox Code Playgroud)

reflection scala

0
推荐指数
1
解决办法
2766
查看次数