1 /*
2 * Copyright (C) 2012-2025 RRiBbit.org
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.rribbit.processing;
17
18 import java.io.Serializable;
19
20 import jakarta.jms.JMSException;
21 import jakarta.jms.Message;
22 import jakarta.jms.MessageListener;
23 import jakarta.jms.ObjectMessage;
24
25 import org.rribbit.Request;
26 import org.rribbit.execution.ListenerObjectExecutor;
27 import org.rribbit.retrieval.ListenerObjectRetriever;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32 * This {@link RequestProcessor} handles {@link Request}s that are sent over JMS.
33 *
34 * @author G.J. Schouten
35 *
36 */
37 public class JmsRequestProcessor extends LocalRequestProcessor implements MessageListener {
38
39 private static final Logger log = LoggerFactory.getLogger(JmsRequestProcessor.class);
40
41 /**
42 * Whenever you use this constructor, be sure to set the {@link ListenerObjectRetriever} AND the {@link ListenerObjectExecutor} with the setters provided by
43 * {@link LocalRequestProcessor}. If you don't, runtime {@link NullPointerException}s will occur.
44 */
45 public JmsRequestProcessor() {}
46
47 /**
48 * This constructor is recommended, since it forces you to specify the {@link ListenerObjectRetriever} and {@link ListenerObjectExecutor}. Passing a null value for either
49 * of these will result in a runtime {@link NullPointerException} whenever the {@link JmsRequestProcessor} is used.
50 *
51 * @param listenerObjectRetriever
52 * @param listenerObjectExecutor
53 */
54 public JmsRequestProcessor(ListenerObjectRetriever listenerObjectRetriever, ListenerObjectExecutor listenerObjectExecutor) {
55 super(listenerObjectRetriever, listenerObjectExecutor);
56 }
57
58 @Override
59 public void onMessage(Message message) {
60
61 log.info("Received Message");
62 if(message instanceof ObjectMessage) {
63 try {
64 Serializable object = ((ObjectMessage) message).getObject();
65 if(object instanceof Request) {
66 this.processRequest((Request) object); //Ignore the response, since JMS is one-direction only
67 } else {
68 throw new IllegalArgumentException("Object must be of type Request");
69 }
70 } catch(JMSException e) {
71 throw new RuntimeException(e);
72 }
73 } else {
74 throw new IllegalArgumentException("Message must be of type ObjectMessage");
75 }
76 }
77 }