[英]JMS connection from Java SE
I would like to create a JMS connection from a Java SE application in a broker-agnostic way. 我想以与代理无关的方式从Java SE应用程序创建JMS连接。
I'm comparing to JDBC with its URL scheme for database connections. 我正在将JDBC与它的URL方案进行数据库连接进行比较。 This creates independence from the actual implementation.
这创建了与实际实现的独立性。
For JMS I haven't found something similar. 对于JMS,我还没有找到类似的东西。 I'm aware that in Java EE the JNDI will fulfill this role, but this is Java SE.
我知道在Java EE中,JNDI将扮演这个角色,但这就是Java SE。
I don't want to tie my code to any particular queue broker as my needs are pretty simple JMS 1.1 send/receive of text messages. 我不想将我的代码绑定到任何特定的队列代理,因为我的需求是非常简单的JMS 1.1发送/接收文本消息。
I've looked at Spring Boot too because it is usually good at providing some level of agnosticism. 我也看过Spring Boot,因为它通常擅长提供某种程度的不可知论性。 But even with Spring Boot, I do not see such possibility.
但是即使使用Spring Boot,我也看不到这种可能性。
JNDI is the way you write your JMS application to connect in a broker-agnostic way. JNDI 是您编写JMS应用程序以与代理无关的方式进行连接的方式。 JNDI client classes are part of Java SE.
JNDI客户端类是Java SE的一部分。 Both Spring and non-Spring Java SE applications use JNDI for this kind of integration.
Spring和非Spring Java SE应用程序都使用JNDI进行这种集成。
Any JMS implementation should also provide a JNDI implementation that can be plugged into your application. 任何JMS实现都应提供可插入您的应用程序的JNDI实现。 Typically this is done by placing a file named
jndi.properties
on your classpath and putting the proper configuration for whatever JNDI implementation you're using into that file. 通常,这是通过在您的类路径上放置一个名为
jndi.properties
的文件,并将您正在使用的JNDI实现的正确配置放入该文件中来完成的。 When you create an empty InitialContext
the jndi.properties
file on your classpath is read automatically. 创建空的
InitialContext
将自动读取类路径上的jndi.properties
文件。 The key=value pairs in jndi.properties
are put into the InitialContext
so that when you perform a lookup everything works with the implementation you've chosen. jndi.properties
中的“键=值”对被放入InitialContext
以便在执行查找时,所有内容都可以与您选择的实现一起使用。 You can also configure this programmatically if you like by supplying the implementation specific details to the InitialContext
via a constructor . 如果愿意,还可以通过编程方式配置此方法,方法是通过构造函数向
InitialContext
提供实现的特定细节。
By using both the JMS and JNDI APIs in your Java SE application and externalizing broker-specific connection details to your jndi.properties
file you can effectively isolate your applications from broker-specific code so you can deploy your app and work with different brokers with a few simple changes in a properties file. 通过在Java SE应用程序中同时使用JMS和JNDI API,并将特定于代理的连接详细信息外部化到
jndi.properties
文件,您可以有效地将应用程序与特定于代理的代码隔离开,从而可以部署应用程序并与其他代理一起使用。属性文件中的一些简单更改。
The JNDI client implementation will come from whoever is providing the JMS implementation. JNDI客户端实现将来自提供JMS实现的任何人。 The JNDI client essentially comes in the form of an
javax.naming.spi.InitialContextFactory
implementation packaged in a jar and there is usually documentation describing the available properties. JNDI客户端本质上是以打包在jar中的
javax.naming.spi.InitialContextFactory
实现的形式出现的,通常会有描述可用属性的文档。
Here are a few examples: 这里有一些例子:
org.apache.activemq.jndi.ActiveMQInitialContextFactory
available in their activemq-client-<version>.jar
. activemq-client-<version>.jar
提供了org.apache.activemq.jndi.ActiveMQInitialContextFactory
activemq-client-<version>.jar
。 Documentation is available here . org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
available in their artemis-jms-client-<version>.jar
. artemis-jms-client-<version>.jar
提供了org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
。 Documentation is available here . To be clear, the JMS specification doesn't require the use of JNDI to look-up admin objects, but it establishes the convention and expectation that JMS providers will do so. 需要明确的是,JMS规范不需要使用JNDI来查找管理对象,但是它建立了约定和期望 ,JMS提供者将这样做。 Section 4.2 of the JMS 1.1 specification states:
JMS 1.1规范的第4.2节规定:
Although the interfaces for administered objects do not explicitly depend on JNDI, JMS establishes the convention that JMS clients find them by looking them up in a namespace using JNDI.
尽管受管理对象的接口并不明确依赖于JNDI,但是JMS建立了约定,即JMS客户端通过使用JNDI在命名空间中查找它们来找到它们。
and later it says: 后来说:
It is expected that JMS providers will provide the tools an administrator needs to create and configure administered objects in a JNDI namespace.
期望JMS提供程序将提供管理员在JNDI名称空间中创建和配置托管对象所需的工具。 JMS provider implementations of administered objects should be both javax.naming.Referenceable and java.io.Serializable so that they can be stored in all JNDI naming contexts.
受管理对象的JMS提供程序实现应该都是javax.naming.Referenceable和java.io.Serializable,以便可以将它们存储在所有JNDI命名上下文中。
In my experience, JMS providers are usually eager to provide a JNDI implementation because they won't be as competitive without it since any alternative solution will not be standards compliant and will force users to implement non-portable code. 以我的经验,JMS提供者通常渴望提供JNDI实现,因为没有它,它们将不具有竞争力,因为任何替代解决方案都将不符合标准,并会迫使用户实现不可移植的代码。
If you run into a provider that doesn't provide a JNDI implementation you could implement your own following the same pattern used by ActiveMQ 5.x , ActiveMQ Artemis , and Qpid JMS . 如果遇到不提供JNDI实现的提供程序,则可以按照ActiveMQ 5.x , ActiveMQ Artemis和Qpid JMS使用的相同模式来实现自己的提供程序。 These 3 implementations are client-side only and simply instantiate the admin objects based on the configuration provided to the
InitialContext
. 这3种实现方式仅在客户端,并且仅基于提供给
InitialContext
的配置实例化管理对象。 Most of the code is boiler plate, and what isn't is very straight-forward. 大多数代码是样板,不是很简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.