gpt4 book ai didi

spring - 如何在 Spring 的 applicationContext 中设置 JMS 配置?

转载 作者:行者123 更新时间:2023-12-01 10:05:34 25 4
gpt4 key购买 nike

如何为下面给出的 java 代码设置应用程序?

Hashtable<String, String> properties = new Hashtable<String, String>();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.test.factory");
properties.put("com.domain", "DevelopmentDomain");
// properties.put(Context.PROVIDER_URL, "tcp://test:0506");
properties.put(Context.PROVIDER_URL, "tcp://10.00.0.00:0506");


properties.put(Context.SECURITY_PRINCIPAL, "aaa");
properties.put(Context.SECURITY_CREDENTIALS, "aaa");

javax.naming.Context context = new javax.naming.InitialContext(properties);
ConnectionFactory factory = (ConnectionFactory) context.lookup("ImpactPocQueueConnectionFactory");

Connection connection = factory.createConnection();

connection.start();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Destination queue = session.createQueue("test.producer");

我在这里和那里看到了很多例子,但没有一个给出完整的画面。

最佳答案

如果您想将此代码逐字转换为 Spring 配置,请考虑利用 @Configuration方法:

@Bean
public Destination queue() throws JMSException, NamingException {
return session().createQueue("test.producer");
}

@Bean
public Session session() throws JMSException, NamingException {
return connection().createSession(false, Session.AUTO_ACKNOWLEDGE);
}

@Bean(initMethod = "start")
public Connection connection() throws JMSException, NamingException {
return connectionFactory().createConnection();
}

@Bean
public ConnectionFactory connectionFactory() throws NamingException {
return (ConnectionFactory) context().lookup("ImpactPocQueueConnectionFactory");
}

@Bean
public Context context() throws NamingException {
return new javax.naming.InitialContext(properties());
}

@Bean
public Hashtable<String, String> properties() {
Hashtable<String, String> properties = new Hashtable<String, String>();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.test.factory");
properties.put("com.domain", "DevelopmentDomain");
//properties.put(Context.PROVIDER_URL, "tcp://test:0506");
properties.put(Context.PROVIDER_URL, "tcp://10.00.0.00:0506");
properties.put(Context.SECURITY_PRINCIPAL, "aaa");
properties.put(Context.SECURITY_CREDENTIALS, "aaa");
return properties;
}

从技术上讲,您可以使用 XML 完成所有这些工作,但我发现这种方法更具可读性和可维护性。现在你有 connectionFactoryqueue范围内可用的 bean 类。您可以轻松地与 Spring JMS support 集成.如果您需要进一步的帮助,请告诉我们。

关于spring - 如何在 Spring 的 applicationContext 中设置 JMS 配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11159232/

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