com.it.j2ee.modules.jms.advanced.AdvancedNotifyMessageProducer.java Source code

Java tutorial

Introduction

Here is the source code for com.it.j2ee.modules.jms.advanced.AdvancedNotifyMessageProducer.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.it.j2ee.modules.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.it.j2ee.modules.permission.entity.User;

/**
 * JMS??.
 * 
 * jmsTemplate????queuetopic.
 * 
 * @author calvin
 */
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() {

            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;
    }
}