2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.jms
;
20 import javax
.jms
.DeliveryMode
;
21 import javax
.jms
.Destination
;
22 import javax
.jms
.JMSException
;
23 import javax
.jms
.Message
;
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
;
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
;
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");
44 jmsTemplate
.convertAndSend(eventsDestination
, event
,
45 new MessagePostProcessor() {
47 public Message
postProcessMessage(Message message
)
49 Map
<String
, String
> headers
= event
.getHeaders();
50 for (String key
: headers
.keySet()) {
51 message
.setStringProperty(key
, headers
.get(key
));
56 if (log
.isTraceEnabled()) {
57 log
.trace("Event " + event
.toString() + " sent to "
58 + eventsDestination
.toString());
63 public void setEventsDestination(Destination eventsDestination
) {
64 this.eventsDestination
= eventsDestination
;
67 public void setJmsTemplate(JmsTemplate jmsTemplate
) {
68 this.jmsTemplate
= jmsTemplate
;