gpt4 book ai didi

tomcat - 在 Tomcat 中为 Tibco EMS 配置 JNDI

转载 作者:行者123 更新时间:2023-11-28 22:00:31 24 4
gpt4 key购买 nike

我已经编写了一些用于从 Tibco EMS 创建主题连接的代码

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");

返回一个空结果

最佳答案

在没有更好的知识的情况下,我创建了自己的 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");

关于tomcat - 在 Tomcat 中为 Tibco EMS 配置 JNDI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9785078/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com