SpringProducer xref

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.spring;
    19
    20  import org.codehaus.activemq.service.Service;
    21  import org.springframework.jms.core.JmsTemplate;
    22  import org.springframework.jms.core.MessageCreator;
    23
    24  import javax.jms.JMSException;
    25  import javax.jms.Message;
    26  import javax.jms.Session;
    27  import javax.jms.TextMessage;
    28
    29  /***
    30   * @version $Revision: 1.1 $
    31   */
    32  public class SpringProducer implements Service {
    33      private JmsTemplate template;
    34      private String subject = getClass().getName();
    35      private int messageCount = 10;
    36
    37
    38      public void start() throws JMSException {
    39          for (int i = 0; i < messageCount; i++) {
    40              final String text = "Text for message: " + i;
    41              template.send(subject, new MessageCreator() {
    42                  public Message createMessage(Session session) throws JMSException {
    43                      System.out.println("Sending message: " + text);
    44                      TextMessage message = session.createTextMessage(text);
    45                      message.setStringProperty("next", "foo");
    46                      return message;
    47                  }
    48              });
    49          }
    50      }
    51
    52      public void stop() throws JMSException {
    53      }
    54
    55      // Properties
    56      //-------------------------------------------------------------------------
    57      public JmsTemplate getTemplate() {
    58          return template;
    59      }
    60
    61      public void setTemplate(JmsTemplate template) {
    62          this.template = template;
    63      }
    64
    65      public int getMessageCount() {
    66          return messageCount;
    67      }
    68
    69      public void setMessageCount(int messageCount) {
    70          this.messageCount = messageCount;
    71      }
    72
    73      public String getSubject() {
    74          return subject;
    75      }
    76
    77      public void setSubject(String subject) {
    78          this.subject = subject;
    79      }
    80  }