Spring ActiveMQ JMS-TopicSession不支持队列

时间:2018-07-19 18:27:52

标签: java spring jms activemq spring-jms

我为使用Spring框架和ActiveMQ作为代理发送JMS消息配置了以下代码。但是,当我尝试将消息发送到目标队列时,出现了以下异常。问题是,如果将“ jmsTemplate ” connectionFactory更改为“ amqConnectionFactory “ ActiveMQ连接工厂。但这不会破坏使用spring缓存连接的目的吗?

知道我在做什么错吗?

例外

org.springframework.jms.InvalidDestinationException: Queues are not supported by a TopicSession; nested exception is javax.jms.InvalidDestinationException: Queues are not supported by a TopicSession

Spring Config

    <!-- A connection to ActiveMQ -->
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL">
        <value>tcp://localhost:61616</value>
    </property>
</bean>

<!-- a cache/pooling based JMS provider -->
<bean id="cachedConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <constructor-arg name="targetConnectionFactory" ref="amqConnectionFactory" />
    <property name="sessionCacheSize" value="5" />
</bean>

<bean id="requestQueue" class="org.apache.activemq.command.ActiveMQQueue">
    <constructor-arg value="EMS_QUEUE" />
</bean>

<!-- Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="cachedConnectionFactory" />
</bean>

<bean id="dataWriterService" class="com.beneris.ems.commons.service.impl.JmsDataWriterService">
    <constructor-arg name="jmsTemplate" ref="jmsTemplate" />
    <constructor-arg name="destination" ref="interrogationDataQueue" />
    <property name="jmsType" value="EMS.DATA.RAW" />
</bean>

Java代码

public class JmsDataWriterService implements DataWriterService {

private JmsTemplate jmsTemplate;
private Destination destination;
private String jmsType;

public JmsDataWriterService(JmsTemplate jmsTemplate, Destination destination) {
    this.jmsTemplate = jmsTemplate;
    this.destination = destination;
}

public void setJmsType(String jmsType) {
    this.jmsType = jmsType;
}

@Override
public void write(String payload) {
    jmsTemplate.send(destination, new MessageCreator() {

        @Override
        public Message createMessage(Session session) throws JMSException {
            TextMessage msg = session.createTextMessage(payload);
            msg.setJMSCorrelationID(Utils.createGuid());
            if(jmsType != null)
                msg.setJMSType(jmsType);
            return msg;
        }
    });
}

@Override
public void write(byte[] payload) {
    jmsTemplate.send(destination, new MessageCreator() {   //throws the EXCEPTION here !!!

        @Override
        public Message createMessage(Session session) throws JMSException {
            BytesMessage msg = session.createBytesMessage();
            msg.writeBytes(payload);
            msg.setJMSCorrelationID(Utils.createGuid());
            if(jmsType != null)
                msg.setJMSType(jmsType);
            return msg;
        }
    });
}

}

0 个答案:

没有答案