我找到了一个example,其中akka-http与Source.single一起用来发出请求。现在我想使用Source.tick来实现每隔X秒执行一次的轮询请求:
import scala.concurrent.duration._
val request: _root_.akka.http.scaladsl.model.HttpRequest = RequestBuilding.Get(Uri("http://api.someSite.com"))
val source: Source[HttpRequest, Cancellable] = Source.tick(1.seconds, 1.seconds, request)
val sourceWithDest = source.via(Http().superPool())
但是,我在最后一行中遇到了编译错误,我无法解决(类型不匹配)。关于我做错了什么或建议替代方案的任何想法?
答案 0 :(得分:2)
根据docs:
Http()返回的流程.superPool(...)非常类似于那个 从主机级客户端API,以及使用主机连接 泳池部分也适用于此。
返回的“池客户端流” Http()。cachedHostConnectionPool(...)具有以下类型:
Flow[(HttpRequest, T), (Try[HttpResponse], T), HostConnectionPool]
这是为了给客户端代码提供实现某些逻辑以将原始请求与相应响应相匹配的可能性。假设在您的情况下不需要这种行为,您可以在将NotUsed
提供给池请求之前将val sourceWithDest: Source[Try[HttpResponse], Cancellable] =
source.map(req ⇒ (req, NotUsed)).via(Http().superPool[NotUsed]()).map(_._1)
附加到池中。 E.g。
<!DOCTYPE html>
<html>
<head>
<title>Fam tree</title>
<script src="http://d3js.org/d3.v2.js"></script>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
}
svg {
position: absolute;
top: 0;
left: 0;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
div.tooltip {
position: absolute;
text-align: center;
width: 200px;
height: 30px;
padding: 8px;
font: 16px sans-serif;
font-weight: bold;
background: #ffff99;
border: solid 1px #aaa;
border-radius: 8px;
pointer-events: none;
background-color: rgba(0,128,128,0.5);
overflow: hidden;
transition: .5s ease
}
</style>
</head>
<body>
<div id="viz"></div>
<script type="text/javascript">
//JSON
var treeData = {"name" : "Steve", "lname" : "Forester", "children" : [
{"name" : "Anna", "lname" : "Woods" },
{"name" : "Boris", "lname" : "Vladimirov" },
{"name" : "Clint", "lname" : "Eastwood", "children": [
{"name" : "Sheldon", "lname" : "Morris" },
{"name" : "Bert", "lname" : "Jefferson" }
]}
]};
// Create a svg canvas
var vis = d3.select("#viz")
.append("svg")
.attr("width", "1500")
.attr("height", "1000")
.call(d3.behavior.zoom().on("zoom", function () {
svg.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")")
}))
.append("g")
.attr("transform", "translate(100, 100)"); // shift everything to the right
// Add tooltip div
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 1e-6);
// Create a tree "canvas"
var tree = d3.layout.tree()
.size([1200, 500]);
var diagonal = d3.svg.diagonal();
// Preparing the data for the tree layout, convert data into an array of nodes
var nodes = tree.nodes(treeData);
// Create an array with all the links
var links = tree.links(nodes);
var link = vis.selectAll("pathlink")
.data(links)
.enter().append("svg:path")
.attr("class", "link")
.attr("d", diagonal)
var node = vis.selectAll("g.node")
.data(nodes)
.enter().append("svg:g")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
// Hexagon img
node.append("svg:image")
.attr("xlink:href", "prva.png")
.attr("width", 100)
.attr("height", 100)
.attr("x", -50)
.attr("y", -30)
.on("mouseover", mouseover)
.on("mousemove", function(d){mousemove(d);})
.on("mouseout", mouseout)
.attr("fill","red")
.attr("r", 5.5);
function mouseover() {
div.transition()
.duration(300)
.style("opacity", 1);
d3.select(this).attr("xlink:href", "vtora.png");
}
function mousemove(d) {
div
.text(" Name:" + ' ' + d.name + ' ' + d.lname)
.style("left", (d3.event.pageX ) + "px")
.style("top", (d3.event.pageY) + "px");
}
function mouseout() {
div.transition()
.duration(300)
.style("opacity", 1e-6);
d3.select(this).attr("xlink:href", "prva.png");
}
</script>
</body>
</html>