com.xyxy.platform.examples.showcase.demos.jms.advanced.AdvancedNotifyMessageProducer.java Source code

Java tutorial

Introduction

Here is the source code for com.xyxy.platform.examples.showcase.demos.jms.advanced.AdvancedNotifyMessageProducer.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.xyxy.platform.examples.showcase.demos.jms.advanced;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import com.xyxy.platform.examples.showcase.entity.User;

/**
 * JMS??.
 * 
 * jmsTemplate????queuetopic.
 * 
 *
 */
public class AdvancedNotifyMessageProducer {

    private JmsTemplate jmsTemplate;
    private Destination notifyQueue;
    private Destination notifyTopic;

    public void sendQueue(final User user) {
        sendMessage(user, notifyQueue);
    }

    public void sendTopic(final User user) {
        sendMessage(user, notifyTopic);
    }

    /**
     * jmsTemplatesend/MessageCreator()??Map?Message?.
     */
    private void sendMessage(final User user, Destination destination) {
        jmsTemplate.send(destination, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {

                MapMessage message = session.createMapMessage();
                message.setString("userName", user.getName());
                message.setString("email", user.getEmail());

                message.setStringProperty("objectType", "user");

                return message;
            }
        });
    }

    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    public void setNotifyQueue(Destination notifyQueue) {
        this.notifyQueue = notifyQueue;
    }

    public void setNotifyTopic(Destination nodifyTopic) {
        this.notifyTopic = nodifyTopic;
    }
}