package com.tszr.service;

import org.springframework.jms.core.MessageCreator;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import java.util.ArrayList;

public class MyMessageService implements MessageCreator {
    @Override
    public Message createMessage(Session session) throws JMSException {
        MapMessage mapm = session.createMapMessage();
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("陈恒1");
        arrayList.add("陈恒2");
        mapm.setObject("mesg1", arrayList);//只能存Java的基本对象
        mapm.setString("mesg2", "测试消息2");
        return mapm;
    }
}

 

package com.tszr;

import com.tszr.service.MyMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.core.JmsTemplate;

@SpringBootApplication
public class Application
implements CommandLineRunner
{ @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args){ SpringApplication.run(Application.class,args); } /** * 我们这里为了方便操作使用run方法发送消息, * 当然完全可以使用控制器通过Web访问 */ public void run(String... args) throws Exception { //new MyMessageService()回调接口方法createMessage产生消息 jmsTemplate.send("mydestination", new MyMessageService()); } }