如何以编程方式创建jms主题和TopicConnectionFactory? [英] How to create a jms Topic and TopicConnectionFactory programatically?

查看:484
本文介绍了如何以编程方式创建jms主题和TopicConnectionFactory?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道你是否可以创建一个主题及其连接工厂有问题?目前我使用glassfish管理工具来创建我的话题和它的连接工厂。如果我不能在代码中创建它,glassfish / openmq会有一个默认的主题和conn工厂,我可以使用它吗?

您只想绕过在admin中手动创建资源,您只需将其部署到文件glassfish-resources.xml(GF 3.1,请参阅 http://docs.oracle.com/cd/E18930_01/html/821-2417/giyhh.html )。



您需要一个像这样的管理对象资源(针对主题):

 < admin-object-resource enabled =truejndi-name =jms / myTopic
object-type =userres-adapter =jmsrares-type =javax.jms。主题>
< property name =Namevalue =physicalTopic/>
< / admin-object-resource>

请注意,您必须为主题使用不同的名称值(此处为physicalTopic)如果实现了多个主题不应混淆消息。



此外,您需要一个连接器资源来引用类型为javax.jms.TopicConnectionFactory的连接器连接池。



如果您不打算使用部署描述符动态创建资源,那么glassfish-resources.xml似乎是最好的选择。



请注意,以这种方式部署的资源是应用程序范围的: http://docs.oracle.com/cd/E18930_01/html/821-2417/giydj.html



glassfish-resources。 xml是GF 3.x的文件,对于GF 2.x,它是sun-resources.xml。如果您使用的是NetBeans,则该文件位于项目视图的服务器资源文件夹中。注意:如果您使用NetBeans进行部署,则服务器资源中的glassfish-resources.xml仅供NetBeans使用! NetBeans知道如何创建这些资源并执行此任务。如果您将EAR直接部署到Glassfish 而没有NetBeans(这看起来很可能用于生产环境) - 您必须在以下位置提供glassfish-resources.xml:


  • 您的EJB模块的META-INF或WAR的模块范围资源的WEB-INF

  • 企业应用程序的应用程序的META-INF全部资源
    在NetBeans中,您通过将文件放入项目视图的配置文件文件夹(文件系统中为src / conf /)来实现此目的。


您可以使用NetBeans的[New Message-Driven Bean]向导轻松创建资源定义(只需通过选择[New ...]来添加MBean)。在向导中选择项目目的地> [添加]。完整的3.1示例如下所示:

 <?xml version =1.0encoding =UTF-8?> ;<!DOCTYPE resources PUBLIC -  // GlassFish.org //DTD GlassFish应用服务器3.1资源定义// ENhttp://glassfish.org/dtds/glassfish-resources_1_5.dtd\"> 
<资源>
< admin-object-resource enabled =truejndi-name =jms / myDestinationres-type =javax.jms.Topicres-adapter =jmsra>
< property name =Namevalue =PhysicalTopic/>
< / admin-object-resource>
< connector-connection-pool name =jms / myDestinationFactoryPoolconnection-definition-name =javax.jms.TopicConnectionFactoryresource-adapter-name =jmsra/>
< connector-resource enabled =truejndi-name =jms / myDestinationFactorypool-name =jms / myDestinationFactoryPool/>
< /资源>

这是MBean注释:

 @MessageDriven(mappedName =java:app / jms / myDestination,activationConfig = 
{
@ActivationConfigProperty(propertyName =acknowledgeMode,propertyValue =Auto-acknowledge ),
@ActivationConfigProperty(propertyName =destinationType,propertyValue =javax.jms.Topic),
@ActivationConfigProperty(propertyName =subscriptionDurability,propertyValue =Durable),
@ActivationConfigProperty(propertyName =clientId,propertyValue =NewMessageBean),
@ActivationConfigProperty(propertyName =subscriptionName,propertyValue =NewMessageBean)
})
public class NewMessageBean implements MessageListener
{
[...]

注意 strong>:只有在使用应用程序范围的资源时,mappedName中的java:app /才是正确的。您可以在glassfish-resources.xml的定义中预留java:app /。 GF部署指南说:应用程序范围内的资源JNDI名称以java:app或
java:module开头,如果其中一个前缀没有在JNDI名称中指定,它将被添加。



您还可以通过使用name而不是mappedName来引入另一个间接级别。然后您必须提供名为application-client.xml的文件,其中(逻辑)名称映射到JNDI物理位置。


Anyone know if you can create a topic and its connection factory problematically? Currently i use the glassfish admin utility to create my topic and its connection factory. If i can't create it in code does glassfish/openmq have a default topic and conn factory i can use?

解决方案

If you only want to circumvent creating the resources manually in the admin you can simply deploy them with the file "glassfish-resources.xml" (GF 3.1, see http://docs.oracle.com/cd/E18930_01/html/821-2417/giyhh.html).

You need an admin-object-resource like this one (for a topic):

<admin-object-resource enabled="true" jndi-name="jms/myTopic"
   object-type="user" res-adapter="jmsra" res-type="javax.jms.Topic">
  <property name="Name" value="physicalTopic"/>
</admin-object-resource>

Be aware that you must use different "Name" values for the Topic (here: "physicalTopic") if you implement multiple Topics whose messages should not get mixed up.

Further you need a connector-resource referencing a connector-connection-pool of type javax.jms.TopicConnectionFactory.

If you don't aim for dynamically creating resources using the deployment descriptor glassfish-resources.xml seems to be the best way.

Please note that resources deployed that way are application-scoped: http://docs.oracle.com/cd/E18930_01/html/821-2417/giydj.html

"glassfish-resources.xml" is the file for GF 3.x, for GF 2.x it was "sun-resources.xml". The file is located in the "Server Resources" folder in your project view if you are using NetBeans. Attention: the glassfish-resources.xml in "Server Resources" is only used by NetBeans if you use NetBeans to deploy! NetBeans knows how to create theses resources and conducts this task. If you deploy an EAR directly to Glassfish without NetBeans -- which seems very likely for a production environment -- you have to provide glassfish-resources.xml in:

  • META-INF of your EJB module or WEB-INF of your WAR for module scoped resources
  • META-INF of your enterprise application for application wide resources In NetBeans you accomplish this by placing the file into the "Configuration Files" folder of your project view (which is src/conf/ in the file system).

You can easily create this resource definition by using NetBeans' [New Message-Driven Bean] wizard (simply add an MBean by selecting [New ...]). In the wizard choose "Project Destinations" > [Add]. A complete 3.1 example looks like this:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
    <admin-object-resource enabled="true" jndi-name="jms/myDestination"  res-type="javax.jms.Topic"  res-adapter="jmsra">
        <property name="Name" value="PhysicalTopic"/>
    </admin-object-resource>
    <connector-connection-pool name="jms/myDestinationFactoryPool"  connection-definition-name="javax.jms.TopicConnectionFactory"  resource-adapter-name="jmsra"/>
    <connector-resource enabled="true" jndi-name="jms/myDestinationFactory" pool-name="jms/myDestinationFactoryPool"  />
</resources>

This is the MBean annotation:

@MessageDriven(mappedName = "java:app/jms/myDestination", activationConfig =
{
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
    @ActivationConfigProperty(propertyName = "clientId", propertyValue = "NewMessageBean"),
    @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "NewMessageBean")
})
public class NewMessageBean implements MessageListener
{ 
[...]

Caution: "java:app/" in mappedName is correct only if you use application scoped resources. You can spare "java:app/" in the definition in glassfish-resources.xml. The GF deployment guide says: "Application-scoped resource JNDI names begin with java:app or java:module. If one of these prefixes is not specified in the JNDI name, it is added."

You can also introduce another level of indirection by using "name" instead of "mappedName". You then have to provide a file named "application-client.xml" where the (logical) name gets mapped to an JNDI "physical" location.

这篇关于如何以编程方式创建jms主题和TopicConnectionFactory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆