我使用Scala中的嵌入式Jetty 9.3.8.v20160314(Scala在这里是偶然的,这是一个纯粹的Jetty问题),配置如下:
val loginService = new RepoLoginService(usersRepository, signInsRepo)
val server = new Server(8080)
server.addBean(loginService)
val security = new ConstraintSecurityHandler()
server.setHandler(security)
val constraint = new Constraint()
constraint.setName("auth")
constraint.setAuthenticate(true)
constraint.setRoles(Array[String]("user", "admin"))
val mapping = new ConstraintMapping()
mapping.setPathSpec("/*")
mapping.setConstraint(constraint)
security.setConstraintMappings(Collections.singletonList(mapping))
security.setAuthenticator(new BasicAuthenticator())
security.setLoginService(loginService)
val mapper = prepareJacksonObjectMapper
val listAccountsController = new ContextHandler("/api/accounts")
listAccountsController.setHandler(new ListAccountsController(mapper, accountsRepository))
val importBankAccountsController = new ContextHandler("/api/bank-account-transactions/import")
importBankAccountsController.setHandler(new ImportBankAccountTransactionsController(mapper, bankAccountTransactionsRepo))
val contexts = new ContextHandlerCollection()
contexts.setHandlers(Array(listAccountsController, importBankAccountsController))
security.setHandler(contexts)
server.start()
server.join()
Run Code Online (Sandbox Code Playgroud)
listAccountsController请注意:的上下文/api/accounts。当我curl时/api/accounts,我收到重定向到/api/accounts/(注意尾部斜杠):
$ curl …Run Code Online (Sandbox Code Playgroud) 我们正在迁移一些东西,需要临时使用两个不同的环境变量附加相同的数据库。现在我们已经迁移了,我想删除附件,但遗憾的是,Heroku 命令行客户端说我有一个不明确的标识符:
\n\n$ heroku addons --app $APP_SOURCE\n\nAdd-on Plan Price State\n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80 \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80 \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80 \xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\nheroku-postgresql (...) hobby-basic $9/month created\n \xe2\x94\x9c\xe2\x94\x80 as DATABASE\n \xe2\x94\x9c\xe2\x94\x80 as HEROKU_POSTGRESQL_ORANGE\n \xe2\x94\x9c\xe2\x94\x80 as DATABASE on stb-crds-rails-sf app\n \xe2\x94\x9c\xe2\x94\x80 as SHARETHEBUS_RAILS_DATABASE on stb-crds-rails-sf app\n \xe2\x94\x94\xe2\x94\x80 as SHARETHEBUS_RAILS_DATABASE_URL on stb-crds-rails-sf app\n\n\n$ heroku addons:detach --app stb-crds-rails-sf $ADDON_NAME\n \xe2\x96\xb8 Ambiguous identifier; multiple matching attachments found: DATABASE, SHARETHEBUS_RAILS_DATABASE, SHARETHEBUS_RAILS_DATABASE_URL.\nRun Code Online (Sandbox Code Playgroud)\n\n我也尝试过heroku addons:detach --app stb-crds-rails-sf $ADDON_NAME --as SHARETHEBUS_RAILS_DATABASE_URL并且heroku addons:detach --app stb-crds-rails-sf $ADDON_NAME SHARETHEBUS_RAILS_DATABASE_URL,但命令行说最后一个参数是意外的。
我们有什么选择来删除额外的插件?
\n我想写一个通用的辅助方法:
def using(closeable: [B has close() method], callback: [B has close() method] => A): A {
try {
callback(closeable)
} finally {
closeable.close()
}
}
Run Code Online (Sandbox Code Playgroud)
意图是我可以使用具有close()方法的任何东西:
using(new FileInputStream(...)) {
stream => stream.read()
}
using(dataSource.getConnection) {
conn => using(conn.createStatement()) {
statement => using(statement.executeQuery("...")) {
rs => rs.getString(1)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是如何命名,以便我可以自己搜索语法,以及语法本身.
我再次与Scala的语法混淆了.我希望这个工作得很好:
// VERSION 1
def isInteractionKnown(service: Service, serviceId: String) = service match {
case TwitterService =>
twitterInteractions.findUuidByTweetId(serviceId.toLong)
case FacebookService =>
facebookInteractions.findUuidByServiceId(serviceId)
}.isDefined
Run Code Online (Sandbox Code Playgroud)
注:这两个findUuidByTweetId和findUuidByServiceId返回Option[UUID]
scalac 告诉我:
error: ';' expected but '.' found.
}.isDefined
Run Code Online (Sandbox Code Playgroud)
当我让我的IDE(IDEA)重新格式化代码时,.isDefined部分结束于它自己的一行.这好像match不是表达.但在我看来,我所做的功能相当于:
// VERSION 2
def isInteractionKnown(service: Service, serviceId: String) = {
val a = service match {
case TwitterService =>
twitterInteractions.findUuidByTweetId(serviceId.toLong)
case FacebookService =>
facebookInteractions.findUuidByServiceId(serviceId)
}
a.isDefined
}
Run Code Online (Sandbox Code Playgroud)
它解析并完全符合我的要求.为什么第一种语法不被接受?
我有一个测试,它模拟了应用程序范围内的回购.大多数时候,测试是绿色的.当我在循环中运行测试时,使用相同的种子,可能有10%的运行成功,但使用GenServer终止消息:
15:39:34.632 [error] GenServer ShortTermMessageStore terminating
** (CaseClauseError) no case clause matching: {:error, %Postgrex.Error{connection_id: 11136, message: nil, postgres: %{code: :foreign_key_violation, constraint: "chat_messages_room_id_ref_fkey", detail: "Key (room_id_ref)=(c78940ab-2514-493e-81fe-64efc63c7bb0) is not present in table \"chat_rooms\".", file: "ri_triggers.c", line: "3324", message: "insert or update on table \"chat_messages\" violates foreign key constraint \"chat_messages_room_id_ref_fkey\"", pg_code: "23503", routine: "ri_ReportViolation", schema: "public", severity: "ERROR", table: "chat_messages", unknown: "ERROR"}}}
(chat_web) lib/chat_web/repo.ex:78: ChatWeb.Repo.append_message_to_store/3
(chat_web) lib/short_term_message_store.ex:23: ChatWeb.ShortTermMessageStore.handle_cast/2
(stdlib) gen_server.erl:601: :gen_server.try_dispatch/4
(stdlib) gen_server.erl:667: :gen_server.handle_msg/5
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: {:"$gen_cast", {:published, "USERID", "c78940ab-2514-493e-81fe-64efc63c7bb0", …Run Code Online (Sandbox Code Playgroud) 我有100分可以分发给一组物品.每个项目必须获得相对于其他项目的比例数量的点数(权重).有些项目的权重可能为0,但必须获得一些积分.
我尝试通过给每个项目5分,然后按比例输出剩余的分数来做到这一点,但是当我有21个项目时,算法将0分配给一个项目.当然,我最初可以提出1点,但问题仍然是101件或更多.通常,这个算法应该处理少于20个项目,但我希望算法在面对更多项目时是健壮的.
我知道使用浮点数/分数是完美的,但底层系统必须接收整数,总数必须为100.
这是框架/语言无关的,虽然我将在Ruby中实现.
目前,我有这个(伪代码):
total_weight = sum_of(items.weight)
if total_weight == 0 then
# Distribute all points equally between each item
items.points = 100 / number_of_items
# Apply remaining points in case of rounding errors (100 / 3 == [33, 33, 34])
else
items.points = 5
points_to_dole_out = 100 - number_of_items * 5
for(item in items)
item.points += item.weight * total_weight / 100
end
end
Run Code Online (Sandbox Code Playgroud) 给出以下Scala代码:
def stream(callback: (String, Array[Byte]) => Unit) {
callback("application/json;charset=utf-8", Array(1))
callback("application/xml;charset=utf-8", Array(1))
}
@Test
def whenStreaming_thenShouldSeeSameMessages() {
var actualMessages = List.empty[(String, Array[Byte])]
sut.stream {
(contentType, payload) =>
actualMessages += (contentType, payload)
}
expect(List("application/json;charset=utf-8", "application/xml;charset=utf-8")) {
actualMessages
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我收到以下错误消息:
error: too many arguments for method +: (other: String)java.lang.String
actualMessages += (contentType, payload)
Run Code Online (Sandbox Code Playgroud)
具体而言,有效负载在IDEA中突出显示,在命令行中,+ =突出显示.我真的不明白这个错误信息.
我正在使用metrics-scala库,并且无法理解为什么下面的两个调用表现完全不同
// method 1
writeTimer.time(() => {
// expensive operation
})
// method 2
writeTimer.time {
// expensive operation
}
Run Code Online (Sandbox Code Playgroud)
在方法1的情况下,从不调用昂贵的操作,而在方法2中,它是.
writeTimer是一个实例com.yammer.metrics.Timer,其中time方法声明为:
/**
* Runs f, recording its duration, and returns the result of f.
*/
def time[A](f: => A): A
Run Code Online (Sandbox Code Playgroud)
我刚刚在我的代码中解决了一个错误,我必须使用方法2来使其工作.
标题说明了一切!我在任何地方都找不到该信息...
谢谢!