2012-03-20 143 views
2

我已經寫了一些代碼,用於創建來自TIBCO EMS配置JNDI爲TIBCO EMS在Tomcat中

TopicConnectionFactory factory = new TibjmsTopicConnectionFactory(serverUrl); 
TopicConnection connection = factory.createTopicConnection(username, password); 
/* if clientID is specified we must set it right here */ 
if (clientID != null) { 
    connection.setClientID(clientID); 
} 

主題連接其中的serverUrl,用戶名,密碼和的clientId從配置文件讀取。我被告知應該使用JNDI來做這件事(我對此非常不瞭解),我需要做什麼?

更新

從各類搜索我想過把這個在context.xml中可能是適當的

<Resource auth="Container" brokerName="not-tibco-952v" brokerURL="tcp://not-tibco-952v:10905" 
      description="JMS Connection Factory" factory="com.tibco.tibjms.naming.TibjmsObjectFactory" 
      name="jms/ProvisioningMessageConnectionFactory" type="com.tibco.tibjms.naming.TibjmsConnectionFactoryAttributes" 
      factoryClass="com.tibco.tibjms.TibjmsConnectionFactory" jndiName="TopicConnectionFactory" serverUrl="not-tibco-952v:10905" 
      username="tibco" password="tibco"/> 

final Object obj = initialContext.lookup("java:comp/env/jms/ProvisioningMessageConnectionFactory"); 

帶回一個空的結果

回答

1

在沒有任何bette的情況下[R知識,我創建了自己的ObjectFactory

public class ProvisioningConnectionsFactory implements ObjectFactory { 
    public Object getObjectInstance(Object obj, 
            Name namex, Context nameCtx, Hashtable environment) 
      throws NamingException { 

    // Acquire an instance of our specified bean class 
    ProvisioningConnection conns = new ProvisioningConnection(); 

    // Customize the bean properties from our attributes 
    Reference ref = (Reference) obj; 
    Enumeration addrs = ref.getAll(); 
    while (addrs.hasMoreElements()) { 
     RefAddr addr = (RefAddr) addrs.nextElement(); 
     String name = addr.getType(); 
     String value = (String) addr.getContent(); 
     if (name.equals("serverUrl")) { 
     conns.setServerUrl(value); 
     } else if (name.equals("username")) { 
     conns.setUsername(value); 
     } else if (name.equals("password")) { 
     conns.setPassword(value); 
     } else if (name.equals("durableTopicSubscriberName")) { 
     conns.setDurableTopicSubscriberName(value); 
     } else if (name.equals("topicName")) { 
     conns.setTopicName(value); 
     } 
    } 

    // Return the customized instance 
    return conns; 

    } 
} 

這意味着我可以添加以下到我的context.xml:

<Resource name="jms/ProvisioningMessageConnection" auth="Container" 
      type="com.foo.mytrialsprovisioning.ProvisioningConnection" 
      factory="com.foo.mytrialsprovisioning.ProvisioningConnectionsFactory" 
      serverUrl = "tcp://not-tibco-952v:10905" 
      username = "tibco" 
      password = "tibco" 
      durableTopicSubscriberName = "PROVISIONING_SUBSCRIBER" 
      topicName = "FOOBAR" 
     /> 

和ProvisioningConnection的一個實例:

public class ProvisioningConnection { 
    private static final Log LOG = LogFactory.getLog(new CurrentClassGetter().getClassName()); 
    private static final String MESSAGE_SELECTOR = "client_ID='%s'"; 
    private String serverUrl; 
    private String username; 
    private String password; 
    private String durableTopicSubscriberName; 
    private String projectIdentifier; 
    private String topicName; 

    public void setServerUrl(String serverUrl) { 
    this.serverUrl = serverUrl; 
    } 

    public void setUsername(String username) { 
    this.username = username; 
    } 

    public void setPassword(String password) { 
    this.password = password; 
    } 

    public void setDurableTopicSubscriberName(String durableTopicSubscriberName) { 
    this.durableTopicSubscriberName = durableTopicSubscriberName; 
    } 

    public void setProjectIdentifier(String projectIdentifier) { 
    this.projectIdentifier = projectIdentifier; 
    } 

    public void setTopicName(String topicName) { 
    this.topicName = topicName; 
    } 

    public TopicConnection getTopicConnection() 
      throws JMSException, NamingException { 
    LOG.info("Provisioning against server: " + serverUrl); 
    TopicConnectionFactory factory = new TibjmsTopicConnectionFactory(serverUrl); 
    TopicConnection connection = factory.createTopicConnection(username, password); 
    /* if clientID is specified we must set it right here */ 
    if (projectIdentifier != null) { 
     connection.setClientID(projectIdentifier); 
    } 
    return connection; 
    } 

    public TopicSubscriber getTopicSubscriber(Session session) 
      throws JMSException { 
    LOG.info("Provisioning subscription on topic: " + topicName); 
    // Use createTopic() to enable subscriptions to dynamic topics. 
    Topic topic = session.createTopic(topicName); 
    return session.createDurableSubscriber(topic, durableTopicSubscriberName, (
      Tools.isNullOrEmptyString(projectIdentifier) ? 
      "" : 
      String.format(MESSAGE_SELECTOR, projectIdentifier) 
    ), true); 
    } 
} 

可以創建使用:

Context envCtx = (Context) initCtx.lookup("java:comp/env"); 
return (ProvisioningConnection)envCtx.lookup("jms/ProvisioningMessageConnection"); 
+3

我不是去接受我自己,因爲我相信那裏有更好的答案 – 2012-03-28 08:50:01

+0

我同意。這應該是一個標準答案。我有個類似的問題。 – 2016-06-07 12:14:57