JmsRequestDispatcher.java
/*
* Copyright (C) 2012-2024 RRiBbit.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.rribbit.dispatching;
import java.util.ArrayList;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.Message;
import jakarta.jms.Queue;
import org.rribbit.Request;
import org.rribbit.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;
/**
* This {@link RequestDispatcher} dispatches {@link Request}s via JMS. It uses the Spring Framework, because they have already done much of the work needed to use JMS. Please note that
* you don't actually have to use the Spring Framework in order to use this class, as long as the necessary Spring libraries are available. See the pom.xml of RRiBbit for the libraries that
* you need.
* <p />
* Dispatching {@link Request}s to the {@link JmsRequestDispatcher} will always yield an empty {@link Response}, because JMS is a one-way protocol. No response will ever come back.
* The {@link Response}s returned by the {@link JmsRequestDispatcher} therefore have an empty list with return values and an empty list with throwables. No null values are returned
* however, so that the caller does not have to take that into account.
* <p />
* For more information, see <a href="https://docs.spring.io/spring-integration/reference/html/jms.html">this page</a>.
*
* @author G.J. Schouten
*
*/
public class JmsRequestDispatcher implements RequestDispatcher {
private static final Logger log = LoggerFactory.getLogger(JmsRequestDispatcher.class);
private JmsTemplate jmsTemplate;
private Queue queue;
/**
* Whenever you use this constructor, be sure to set the {@link Queue} AND the {@link ConnectionFactory} with the setters provided by this class.
* If you don't, runtime exceptions will occur.
*/
public JmsRequestDispatcher() {}
/**
* Creates a new {@link JmsRequestDispatcher} that dispatches {@link Message}s to the given {@link Queue}, using the given {@link ConnectionFactory}.
*
* This constructor is recommended, since it forces you to specify the {@link Queue} and {@link ConnectionFactory}. Passing a null value for either
* of these will result in runtime exceptions whenever the {@link JmsRequestDispatcher} is used.
*
* @param connectionFactory
* @param queue
*/
public JmsRequestDispatcher(ConnectionFactory connectionFactory, Queue queue) {
this.setConnectionFactory(connectionFactory);
this.setQueue(queue);
}
@Override
public <T> Response<T> dispatchRequest(Request request) {
log.info("Sending Request to JMS Queue");
jmsTemplate.send(queue, session -> session.createObjectMessage(request));
log.info("Returning empty Response");
return new Response<>(new ArrayList<>(), new ArrayList<>());
}
public void setConnectionFactory(ConnectionFactory connectionFactory) {
this.jmsTemplate = new JmsTemplate(connectionFactory);
}
public void setQueue(Queue queue) {
this.queue = queue;
}
}