KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > spring > SpringProducer


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.spring;
19
20 import org.springframework.jms.core.JmsTemplate;
21 import org.springframework.jms.core.MessageCreator;
22
23 import javax.jms.Destination JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.Message JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.jms.TextMessage JavaDoc;
28
29 public class SpringProducer {
30     
31     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
32             .getLog(SpringProducer.class);
33     
34     private JmsTemplate template;
35     private Destination JavaDoc destination;
36     private int messageCount = 10;
37
38
39     public void start() throws JMSException JavaDoc {
40         for (int i = 0; i < messageCount; i++) {
41             final String JavaDoc text = "Text for message: " + i;
42             template.send(destination, new MessageCreator() {
43                 public Message JavaDoc createMessage(Session JavaDoc session) throws JMSException JavaDoc {
44                     log.info("Sending message: " + text);
45                     TextMessage JavaDoc message = session.createTextMessage(text);
46                     message.setStringProperty("next", "foo");
47                     return message;
48                 }
49             });
50         }
51     }
52
53     public void stop() throws JMSException JavaDoc {
54     }
55
56     // Properties
57
//-------------------------------------------------------------------------
58

59     public JmsTemplate getTemplate() {
60         return template;
61     }
62
63     public void setTemplate(JmsTemplate template) {
64         this.template = template;
65     }
66
67     public int getMessageCount() {
68         return messageCount;
69     }
70
71     public void setMessageCount(int messageCount) {
72         this.messageCount = messageCount;
73     }
74
75     public Destination JavaDoc getDestination() {
76         return destination;
77     }
78
79     public void setDestination(Destination JavaDoc destination) {
80         this.destination = destination;
81     }
82 }
83
Popular Tags