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

问题描述

任何人都知道您是否可以以编程方式创建主题及其连接吗?当前,我使用glassfish管理实用程序来创建主题及其连接工厂.如果我无法在代码中创建它,那么glassfish/openmq是否具有默认主题以及可以使用的conn工厂?

Anyone know if you can create a topic and its connection factory programmatically? 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?

推荐答案

如果您只想规避在管理员中手动创建资源,则只需使用文件"glassfish-resources.xml"(GF 3.1,请参见 http://docs.oracle.com/cd/E18930_01/html /821-2417/giyhh.html ).

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>

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

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.

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

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

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

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

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

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

"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:

  • 您的EJB模块的META-INF或WAR的WEB-INF用于模块范围的资源
  • 企业应用程序的
  • META-INF以获取应用程序范围的资源在NetBeans中,您可以通过将文件放入项目视图的配置文件"文件夹(在文件系统中为src/conf/)来实现此目的.
  • 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 resourcesIn 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).

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

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>

这是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
{ 
[...]

注意:仅当您使用应用程序范围内的资源时,mappedName中的"java:app/"才正确.您可以在glassfish-resources.xml中的定义中保留"java:app/". GF部署指南说:应用程序范围的资源JNDI名称以java:app或java:模块.如果未在JNDI名称中指定这些前缀之一,则将其添加."

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."

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

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 Topic和TopicConnectionFactory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 13:29