]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsSlcEventPublisher.java
Move to SLC legacy
[gpl/argeo-slc.git] / legacy / runtime / org.argeo.slc.support.activemq / src / main / java / org / argeo / slc / jms / JmsSlcEventPublisher.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 * http://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.argeo.slc.jms;
17
18 import java.util.Map;
19
20 import javax.jms.DeliveryMode;
21 import javax.jms.Destination;
22 import javax.jms.JMSException;
23 import javax.jms.Message;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.slc.SlcException;
28 import org.argeo.slc.msg.event.SlcEvent;
29 import org.argeo.slc.msg.event.SlcEventPublisher;
30 import org.springframework.jms.core.JmsTemplate;
31 import org.springframework.jms.core.MessagePostProcessor;
32
33 public class JmsSlcEventPublisher implements SlcEventPublisher {
34 private static final Log log = LogFactory
35 .getLog(JmsSlcEventPublisher.class);
36 private Destination eventsDestination;
37 private JmsTemplate jmsTemplate;
38
39 public void publish(final SlcEvent event) {
40 if (jmsTemplate.getDeliveryMode() != DeliveryMode.PERSISTENT)
41 throw new SlcException(
42 "Delivery mode has to be persistent in order to have durable subscription");
43
44 jmsTemplate.convertAndSend(eventsDestination, event,
45 new MessagePostProcessor() {
46
47 public Message postProcessMessage(Message message)
48 throws JMSException {
49 Map<String, String> headers = event.getHeaders();
50 for (String key : headers.keySet()) {
51 message.setStringProperty(key, headers.get(key));
52 }
53 return message;
54 }
55 });
56 if (log.isTraceEnabled()) {
57 log.trace("Event " + event.toString() + " sent to "
58 + eventsDestination.toString());
59 }
60
61 }
62
63 public void setEventsDestination(Destination eventsDestination) {
64 this.eventsDestination = eventsDestination;
65 }
66
67 public void setJmsTemplate(JmsTemplate jmsTemplate) {
68 this.jmsTemplate = jmsTemplate;
69 }
70
71 }