簡體   English   中英

Spring jms activemq

[英]Spring jms activemq

我使用activemq和spring jms。 我有個問題。

這是我的EmailSender

@Service
@PropertySource("classpath:/properties/dev/application.properties")
public class EmailSenderImpl implements EmailSender {
    private static final Logger LOGGER = LoggerFactory.getLogger(EmailSenderImpl.class);

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Environment env;

    @Override
    public void sendEmail(final Users user, EmailCauses emailCause) {
        LOGGER.debug("Sending email to user with id {} with cause {}", user.getId(), emailCause);

        //Retrieve User profile which defined with lazy fetch type.
        //It prevents org.hibernate.LazyInitializationException in jms message receiver method
        user.getUserProfile();

        jmsTemplate.send(env.getProperty(emailCause.getEmailQueueName()),
                new MessageCreator () {

                    @Override
                    public Message createMessage(Session session) throws JMSException {
                        return session.createObjectMessage(user);
                    }
                }
        );  
    } 

這是我的EmailHandler。

@Service
public class EmailHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(EmailHandler.class);

    private JavaMailSender mailSender;
    private VelocityEngine velocityEngine;
    private MessageSource messageSource;


    @Autowired
    public EmailHandler (JavaMailSender mailSender, VelocityEngine velocityEngine, MessageSource messageSource) {

        this.mailSender = mailSender;
        this.velocityEngine = velocityEngine;
        this.messageSource = messageSource;
    }


    //TODO - refactor method, add reserve server
    @Transactional
    public void processEmailNotifications (Users user) throws MessagingException, UnsupportedEncodingException {
        LOGGER.debug("Preparing to send greeting email to user {}", user);

        UserProfiles userProfile = user.getUserProfile();

        Map <String, Object> model = new HashMap <> ();
        model.put("name", userProfile.getName());
        model.put("patronymic", userProfile.getPatronymic());
        model.put("token", UriUtils.encode(user.getConfirmationToken(), "UTF8"));

        String email = user.getUserProfile().getEmail();
        sendEmail(email, "email.wellcome.subject", model, "wellcome_email.vm");

    }
     private void sendEmail (String emailTo, String subjectMessage, Map<String, Object> model, String templateName) {
        LOGGER.debug("Sending email to {} with subject message {}", emailTo, subjectMessage);

        try {

            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper (message, true, "UTF-8");

            helper.setTo(emailTo);
            helper.setSubject(messageSource.getMessage(subjectMessage, null, Locale.getDefault()));

            String emailText = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, "UTF-8", model);
            helper.setText(emailText, true);

            mailSender.send(message);
            LOGGER.info("Email to address {} with subject {} was sent successfully", emailTo, subjectMessage);

        } catch (MessagingException me) {
            LOGGER.error("Email to address {} was not sent", emailTo, me);

        }

    }

我的堆棧跟蹤中有以下日志。 我不明白為什么它不起作用。

osjms.core.JmsTemplate - 在JMS會話上執行回調:ActiveMQSession {id = ID:igor-Aspire-5820TG-33061-1474030841626-1:7:1,started = false} 2016-09-16 16:02:40.871 - [ http-bio-8080-exec-6] [DEBUG] osjms.core.JmsTemplate - 發送創建的消息:ActiveMQTextMessage {commandId = 0,responseRequired = false,messageId = null,originalDestination = null,originalTransactionId = null,producerId = null,destination = null,transactionId = null,expiration = 0,timestamp = 0,arrival = 0,brokerInTime = 0,brokerOutTime = 0,correlationId = null,replyTo = null,persistent = false,type = null,priority = 0,groupID = null ,groupSequence = 0,targetConsumerId = null,compressed = false,userID = null,content = null,marshalledProperties = null,dataStructure = null,redeliveryCounter = 0,size = 0,properties = null,readOnlyProperties = false,readOnlyBody = false,droppable = false,text = xDDD}

你可以發布其他日志和JmsTemplate設置配置,因為會話沒有在你的日志和生產者,messageid和目的地上啟動...信息是null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM