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