Java 用Spring配置tibco jms
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2848659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configuration of tibco jms with Spring
提问by dbf
Now I have java code which starts tibco jms subscription with several steps:
现在我有 java 代码,它通过几个步骤启动 tibco jms 订阅:
Properties props = new Properties();
props.put("java.naming.provider.url", namingProvider);
props.put("java.naming.factory.initial", namingFactory);
props.put("java.naming.referral", namingReferral);
props.put("java.naming.security.credentials", securityCredentials);
props.put("java.naming.security.principal", securityPrincipal);
context = new InitialContext(props);
TopicConnectionFactory tfactory = (TopicConnectionFactory) context
.lookup(url + topic + ":tcf");
topicConnection = tfactory.createTopicConnection(user, password);
topicConnection.setExceptionListener(exceptionListener);
topicSession = topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
subscriber = topicSession.createSubscriber(
(Topic) context.lookup(url + topic + ":topic"),
SELECTOR, true);
subscriber.setMessageListener(messageListener);
topicConnection.start();
I want to use Spring for initialization, but I can't find how to configure jms in spring using initial context. Can you help me with it? Thanks!
我想使用 Spring 进行初始化,但是我找不到如何使用初始上下文在 spring 中配置 jms。你能帮我解决吗?谢谢!
采纳答案by Eugene Kuleshov
You can try something like this to get TopicConnectionFactory:
你可以尝试这样的事情来获得 TopicConnectionFactory:
<bean id="tfactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="url/topic:tcf"/>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.provider.url">...</prop>
<prop key="java.naming.factory.initial">...</prop>
<prop key="java.naming.referral">...</prop>
<prop key="java.naming.security.credentials">...</prop>
<prop key="java.naming.security.principal">...</prop>
</props>
</property>
</bean>
回答by Parthasarathy B
Good Day Friends,
朋友们好,
Step 1:
第1步:
As of today you have to be registered to use https://download.tibco.com/tibco.
从今天起,您必须注册才能使用https://download.tibco.com/tibco。
You can download a trial of EMS at http://tap.tibco.com/storefront/trialware/tibco-enterprise-message-service/prod15032.html. Once you install the trial the jars will be in the ems/8.0/libdirectory.
您可以在http://tap.tibco.com/storefront/trialware/tibco-enterprise-message-service/prod15032.html下载 EMS 试用版 。安装试用版后,jar 将位于ems/8.0/lib目录中。
Step 2:
第2步:
Tibco jar files are not available @ Maven Repository.
Tibco jar 文件在@Maven 存储库中不可用。
Collect all tibco jar files from tibco ems installables.
从 tibco ems installables 收集所有 tibco jar 文件。
- jms.jar - Available @ Maven repo
- tibjms.jar
- tibcrypt.jar (For SSL)
- tibemsd_sec.jar
- tibjms.jar
- tibjmsadmin.jar
- tibjmsapps.jar
- tibjmsufo.jar
- tibrvjms.jar
- spring-jms.jar - Available @ Maven repo
- jms.jar -可用@Maven repo
- tibjms.jar
- tibcrypt.jar(用于 SSL)
- tibemsd_sec.jar
- tibjms.jar
- tibjmsadmin.jar
- tibjmsapps.jar
- tibjmsufo.jar
- tibrvjms.jar
- spring-jms.jar -可用 @ Maven repo
Execute below command from command prompt to include tibco jars into local maven repository.
从命令提示符执行以下命令以将 tibco jar 包含到本地 maven 存储库中。
Step 3:
第 3 步:
Commands:
命令:
mvn install:install-file -DgroupId=javax.jms -DartifactId=jms -Dversion=1.1 -Dpackaging=jar -Dfile=jms.jar mvn install:install-file -DgroupId=com.tibco -DartifactId=tibcrypt -Dversion=5.1.0 -Dpackaging=jar -Dfile=tibcrypt.jar mvn install:install-file -DgroupId=com.tibco -DartifactId=tibemsd_sec -Dversion=5.1.0 -Dpackaging=jar -Dfile=tibemsd_sec.jar mvn install:install-file -DgroupId=com.tibco -DartifactId=tibjms -Dversion=5.1.0 -Dpackaging=jar -Dfile=tibjms.jar mvn install:install-file -DgroupId=com.tibco -DartifactId=tibjmsadmin -Dversion=5.1.0 -Dpackaging=jar -Dfile=tibjmsadmin.jar mvn install:install-file -DgroupId=com.tibco -DartifactId=tibjmsapps -Dversion=5.1.0 -Dpackaging=jar -Dfile=tibjmsapps.jar mvn install:install-file -DgroupId=com.tibco -DartifactId=tibjmsufo -Dversion=5.1.0 -Dpackaging=jar -Dfile=tibjmsufo.jar mvn install:install-file -DgroupId=com.tibco -DartifactId=tibrvjms -Dversion=5.1.0 -Dpackaging=jar -Dfile=tibrvjms.jar
mvn install:install-file -DgroupId=javax.jms -DartifactId=jms -Dversion=1.1 -Dpackaging=jar -Dfile=jms.jar mvn install:install-file -DgroupId=com.tibco -DartifactId=tibcrypt -Dversion=5.1 .0 -Dpackaging=jar -Dfile=tibcrypt.jar mvn install:install-file -DgroupId=com.tibco -DartifactId=tibemsd_sec -Dversion=5.1.0 -Dpackaging=jar -Dfile=tibemsd_sec.jar mvn install:install-file -DgroupId=com.tibco -DartifactId=tibjms -Dversion=5.1.0 -Dpackaging=jar -Dfile=tibjms.jar mvn install:install-file -DgroupId=com.tibco -DartifactId=tibjmsadmin -Dversion=5.1.0 -Dpackaging =jar -Dfile=tibjmsadmin.jar mvn install:install-file -DgroupId=com.tibco -DartifactId=tibjmsapps -Dversion=5.1.0 -Dpackaging=jar -Dfile=tibjmsapps.jar mvn install:install-file -DgroupId=com .tibco -DartifactId=tibjmsufo -Dversion=5.1.0 -Dpackaging=jar -Dfile=tibjmsufo.jar mvn 安装:安装文件 -DgroupId=com.tibco -DartifactId=tibrvjms -Dversion=5.1.0 -Dpackaging=jar -Dfile=tibrvjms.jar
Maven Dependencies for Tibco EMS. Add it on you .pom file dependencies
Tibco EMS 的 Maven 依赖项。将它添加到您的 .pom 文件依赖项上
<!-- Tibco EMS Start -->
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.tibco</groupId>
<artifactId>tibcrypt</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.tibco</groupId>
<artifactId>tibemsd_sec</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.tibco</groupId>
<artifactId>tibjms</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.tibco</groupId>
<artifactId>tibjmsadmin</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.tibco</groupId>
<artifactId>tibjmsapps</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.tibco</groupId>
<artifactId>tibjmsufo</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.tibco</groupId>
<artifactId>tibrvjms</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- Tibco EMS End -->
Shown is the Spring file for tibco config.
显示的是 tibco 配置的 Spring 文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<context:annotation-config />
<context:property-placeholder
location="file:${user.home}/config.properties"
ignore-resource-not-found="true" system-properties-mode="OVERRIDE" />
<context:component-scan base-package="com.mycompany.ems" />
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">com.tibco.tibjms.naming.TibjmsInitialContextFactory</prop>
<prop key="java.naming.provider.url">tcp://xx.xx.xx.xx:xxxx</prop> <!--dynamic depending on your project-->
</props>
</property>
</bean>
<bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="QueueConnectionFactory" />
</bean>
<bean id="jmsDestinationResolver"
class="org.springframework.jms.support.destination.JndiDestinationResolver">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="cache" value="true" />
</bean>
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="xxxxxx" /> <!--dynamic depending on your project-->
</bean>
<bean id="authenticationConnectionFactory"
class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory" ref="jmsConnectionFactory" />
<property name="username" value="xxxxxx" /> <!--dynamic depending on your project-->
<property name="password" value="xxxxxx" /> <!--dynamic depending on your project-->
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="authenticationConnectionFactory" />
<property name="destinationResolver" ref="jmsDestinationResolver" />
<property name="defaultDestination" ref="destination" />
</bean>
<bean id="logger" class="com.mycompany.ems.Logger" />
Java File Implementation
Java文件实现
package com.mycompany.ems;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
/**
* @author Parthasarathy Balakrishnan
* @description sends tibco queue message from this class
*
*/
@Component
public class Logger{
private final static Logger logger = Logger
.getLogger(Logger .class);
private static JmsTemplate jmsTemplate;
/**
*
*/
public Logger() {
}
/**
* @param <T>
* @param args
* @author Parthasarathy Balakrishnan
*
*/
public static <T> void log(Class<T> clazz, Object message, Throwable t) {
try {
JMSSend("PROJECT_QUEUE_NAME", clazz, message, t);
logger.debug("Message Sent...");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param Queue
* @param clazz
* @param message
* @param t
* @Description It uses org.springframework.jms.core.JmsTemplate to send JMS message to tibco Queue.
* @author Parthasarathy Balakrishnan
*/
static <T> void JMSSend(String Queue, final Class<T> clazz, final Object message,
final Throwable t) {
jmsTemplate.send(Queue, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage sendMessage = session
.createTextMessage("<xml>send message</xml>");
return sendMessage;
}
});
}
/**
* @return
*/
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
/**
* @param jmsTemplate
*/
@Autowired
public void setJmsTemplate(JmsTemplate jmsTemplate) {
BsfLogger.jmsTemplate = jmsTemplate;
}
}
It is working for me, Please ping me in case of any exceptions.
它对我有用,如果有任何异常,请ping我。