我很想知道其他人如何处理使用官方RabbitMQ java客户端库从错误连接中恢复的问题.我们正在使用它将我们的应用程序服务器连接到我们的RabbitMQ集群,我们已经实现了几种不同的方法来从连接失败中恢复,但是他们感觉不是很正确.
想象一下这个伪应用程序:
public class OurClassThatStartsConsumers {
Connection conn;
public void start() {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("someusername");
factory.setPassword("somepassword");
factory.setHost("somehost");
conn = factory.newConnection();
new Thread(new Consumer(conn.createChannel())).start();
}
}
class Consumer1 implements Runnable {
public Consumer1(Channel channel) {
this.channel = channel;
}
@Override
public void run() {
while (true) {
... consume incoming messages on the channel...
// How do we handle that the connection dies?
}
}
}
Run Code Online (Sandbox Code Playgroud)
在现实世界中,我们有数百名消费者.那么如果连接死了怎么办?在上面的示例中,Consumer1无法恢复,当连接关闭时,Channel也会关闭,这是我们无法恢复的状态.那么让我们看看解决这个问题的一些方法:
解决方案A)
让每个消费者拥有自己的连接,并注册连接死亡时触发的事件,然后处理重新连接.
优点:它的工作原理
缺点:
解决方案B)
让每个消费者使用相同的连接并订阅它的连接失败事件.
优点:比解决方案A更少的连接
缺点:由于连接已关闭,我们需要重新打开/替换它.java客户端库似乎没有提供重新打开连接的方法,因此我们必须用新连接替换它,然后以某种方式通知所有消费者这个新连接,他们将不得不重新创建频道和消费者.再一次,我不希望在消费者中看到的许多逻辑最终存在. …
我正在Netbeans平台上开发一个编程工具.在那里,我有一个动作来查找用法,我想添加Alt+ F7作为触发我的动作的快捷方式.我已经为Alt+ F3和其他一些快捷方式实现了这个.
但在这种情况下,Alt+ F7已经在Netbeans平台中用于查找用法.
我怎样才能覆盖Alt+ F7?
这是我已经做过的事情:
@ActionRegistration(displayName = "#CTL_FindUsagesAction")
@ActionReferences(value = {
@ActionReference(path = "Shortcuts", name = "A-F7"),
Run Code Online (Sandbox Code Playgroud) 在Groovy中,您可以使用as
运算符或asType
方法进行令人惊讶的类型转换.例子包括
Short s = new Integer(6) as Short
List collection = new HashSet().asType(List)
Run Code Online (Sandbox Code Playgroud)
我很惊讶我可以从Integer转换为Short并从Set转换为List,因为这些类型之间没有"是"关系,尽管它们共享一个共同的祖先.
例如,就转换中涉及的类型之间的关系而言,以下代码等同于Integer/Short示例
class Parent {}
class Child1 extends Parent {}
class Child2 extends Parent {}
def c = new Child1() as Child2
Run Code Online (Sandbox Code Playgroud)
但当然这个例子失败了.as
运算符和asType
方法背后的类型转换规则究竟是什么?
在groovy中获取当前日期和时间的代码是什么?我环顾四周,找不到一个简单的方法来做到这一点.基本上我正在寻找相当于Linux的date
我有 :
import java.text.SimpleDateFormat
def call(){
def date = new Date()
sdf = new SimpleDateFormat("MM/dd/yyyy")
return sdf.format(date)
}
Run Code Online (Sandbox Code Playgroud)
但我也需要打印时间.
如果在其中运行的服务返回不同于的退出代码,是否可以在docker-compose中重新启动容器0
?docker-compose.yml选项restart: always
不起作用.有没有办法解决它或这是一个服务问题,我应该寻找容器内的答案?
我使用supervisord但是autorestart=true
即使服务因退出代码崩溃,添加选项也不起作用255
- RUNNING_PID
文件(由系统创建)没有被删除.
谢谢你的回复.
我有像这样的html结构.我想得到没有类或id的第二个表.我如何从第二张表中获取iframe
?
<iframe>
<html>
<body>
<table><table>
<table>
<tr><td></td></tr>
<tr><td></td></tr>
</table>
</body>
</html>
</iframe>
Run Code Online (Sandbox Code Playgroud)
我这样想
Elements iframe = doc.select("iframe");
for(Element e : iframe) {
System.out.println(e.child(0));
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我?
我有以下来自 Spring 框架项目的工作 groovy 代码:
import org.springframework.oxm.Unmarshaller
public class ItemSearchService {
Unmarshaller unmarshaller;
public ItemSearchResponse getObject(InputStream xml) {
ItemSearchResponse its = null;
try {
its = (ItemSearchResponse) unmarshaller.unmarshal(new StreamSource(xml));
} finally {
}
return its;
}
}
Run Code Online (Sandbox Code Playgroud)
Unmarshaller.unmarshall 其实就是接口方法:https : //docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/oxm/Unmarshaller.html
Unmarshaller 接口由几个类实现。
谁决定在运行时注入哪个实现类以及它如何决定使用哪个类?
Spring IoC 机制有这个功能吗?如果是这样,它是在生成 jar 的构建时间期间选择一个特定的实现类,还是在运行时执行?
另外如何知道它实际使用了哪个实现类?
假设类路径中有依赖的 jar,上面的代码可以在普通 Java 文件中的 Spring 之外工作吗?
我目前正在编写 gradle 构建脚本。什么是委托对象以及它们何时使用?
class GroovyGreeter {
String greeting = "Default greeting"
def printGreeting(){println "Greeting: $greeting"}
}
def myGroovyGreeter = new GroovyGreeter()
myGroovyGreeter.printGreeting()
myGroovyGreeter.greeting = "My custom greeting"
myGroovyGreeter.printGreeting()
/*
The last Groovy feature we'll cover is that closures can have a delegate
object. Any variables or methods referenced in the closure that don't have a
local definition are then evaluated against the closure's delegate. Let's make
a closure that will access the property and method of our GroovyGreeter class.
*/ …
Run Code Online (Sandbox Code Playgroud)