我们已经在我们的batch账户上启用了诊断功能,以将事件流式传输到我们在应用程序中捕获的event hub,以便基于batch任务状态采取行动。然而,我们注意到连接会自动关闭(可能是因为晚上没有事件发生),因此我们必须每隔一段时间重新启动服务器,才能再次接收事件/消息。
我们仍然依赖于java 7,下面是我们为批处理添加的依赖项:
//azure dependency
compile('com.microsoft.azure:azure-storage:7.0.0')
compile('com.microsoft.azure:azure-batch:5.0.1') {
//do not get transitive dependency com.nimbusds:nimbus-jose-jw because spring security still rely on old version of it
excludes group: 'com.nimbusds', module: 'nimbus-jose-jw'
}
compile('com.fasterxml.jackson.core:jackson-core:2.9.8')
compile('org.apache.qpid:qpid-amqp-1-0-common:0.32')
compile('org.apache.qpid:qpid-amqp-1-0-client:0.32')
compile('org.apache.qpid:qpid-amqp-1-0-client-jms:0.32')
compile('org.apache.qpid:qpid-jms-client:0.40.0')
compile('org.apache.geronimo.specs:geronimo-jms_1.1_spec:1.1.1')
//end of azure dependency这是进行连接的代码片段,实际上我们使用了这里给出的代码示例:http://theitjourney.blogspot.com/2015/12/sendreceive-messages-using-amqp-in-java.html,因为我们在azure文档本身中找不到任何适用于java7的示例。
/**
* Set up connection to the service bus using AMQP mechanism.
* NOTE: Messages received from the message bus are not guaranteed to follow order.
* */
MessageConsumer initiateConsumer(MessageListener messageListener, Integer partitionInx, BatchEventHubConfig batchEventHubConfig) {
// set up JNDI context
String queueName = "EventHub"
String connectionFactoryName = "SBCFR"
Hashtable<String, String> hashtable = new Hashtable<>()
hashtable.put("connectionfactory.${connectionFactoryName}", batchEventHubConfig.getAMQPConnectionURI())
hashtable.put("queue.${queueName}", "${batchEventHubConfig.name}/ConsumerGroups/${batchEventHubConfig.consumerGroup}/Partitions/${partitionInx}")
hashtable.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.amqp_1_0.jms.jndi.PropertiesFileInitialContextFactory")
Context context = new InitialContext(hashtable)
ConnectionFactory factory = (ConnectionFactory) context.lookup(connectionFactoryName)
Destination queue = (Destination) context.lookup(queueName)
Connection connection = factory.createConnection(batchEventHubConfig.sasPolicyName, batchEventHubConfig.sasPolicyKey)
connection.setExceptionListener(new BatchExceptionListener())
connection.start()
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)
MessageConsumer messageConsumer = session.createConsumer(queue)
messageConsumer.setMessageListener(messageListener)
messageConsumer
}那么,是否有一种方法可以跟踪连接是否关闭,如果是,是否可以重新启动连接?
任何进一步诊断此问题的信息也将不胜感激。
发布于 2019-04-24 19:36:34
我想我找到了问题所在,我使用了"SBCFR“作为connectionFactoryName,仔细观察链接中的示例,我应该使用"SBCF”。我还将lib "org.apache.qpid:qpid-jms-client“从版本"0.40.0”更新为"0.41.0“。
另外,在上面的代码中,我不应该使用AUTO_ACKNOWLEGDE,因为在很长一段时间内,我都认为出了问题,因为我从未在本地设置中接收到事件。事实证明,其他机器也连接到相同的消费者组,并且已经确认了消息。
https://stackoverflow.com/questions/55813135
复制相似问题