前提
以下の2通りのMQ送信例で動作する事は確認できています。
実現したいこと
XML定義の方のやり方で送信先Queue名を動的に切り替えたいと思ってます。
発生している問題・エラーメッセージ
動的に切り替え可能か、検討している。
(<int-jms:outbound-channel-adapter>のdestinationがシングルトンでないかの確認含む)
該当のソースコード
Java
1●Javaで動的にQueueを切り替えたい場合 2 public String queue(QueueRequest input) throws JMSException { 3 QueueSender sender = null; 4 QueueSession session = null; 5 QueueConnection connection = null; 6 try { 7 //Connectionを作成するFactoryを作成 8 QueueConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL); 9 connection = factory.createQueueConnection(); 10 11 //セッションの作成 12 session = connection.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE); 13 Queue queue = session.createQueue("送信するQueue名指定"); 14 sender = session.createSender(queue); 15 16 connection.start(); 17 Message msg = session.createTextMessage(StringUtil.fixedLengthString(input.getName(), 40)); 18 19 //メッセージの送信 20 sender.send(msg); 21 } catch(JMSException e) {
XML(Spring)
1●XML定義の場合 2 <!-- JMS設定 --> 3 <bean id="jmsConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> 4 <property name="targetConnectionFactory"> 5 <bean class="org.apache.activemq.ActiveMQConnectionFactory"> 6 <property name="brokerURL" value="${mq.url}" /> 7 </bean> 8 </property> 9 <property name="sessionCacheSize" value="10"/> 10 <property name="cacheConsumers" value="false"/> 11 </bean> 12 <bean id="requestQueue1" class="org.apache.activemq.command.ActiveMQQueue">←消したい 13 <constructor-arg value="TestQueue"/> 14 </bean> 15 <bean id="requestQueue2" class="org.apache.activemq.command.ActiveMQQueue">←消したい 16 <constructor-arg value="SampleQueue"/> 17 </bean> 18 19 <int-jms:outbound-channel-adapter channel="request_amq" destination="requestQueue2" connection-factory="jmsConnectionFactory"/>←Queue(destination)名の固定指定を辞めたい 20 <int-jms:outbound-channel-adapter channel="request_amq" destination="requestQueue1" connection-factory="jmsConnectionFactory"/>``` 21 22### 試したこと 23 24https://spring.pleiades.io/spring-integration/docs/current/reference/html/jms.html#jms-outbound-gateway 25リファレンスを見て<int-jms:outbound-gateway>なら実現できそうか確認。 26 27### 補足情報(FW/ツールのバージョンなど) 28SpringBoot2.6.6 29Jms2.0.1 30ActiveMQ5.17.0 31Java8 or 11
あなたの回答
tips
プレビュー