Example usage for javax.jms Session createObjectMessage

List of usage examples for javax.jms Session createObjectMessage

Introduction

In this page you can find the example usage for javax.jms Session createObjectMessage.

Prototype


ObjectMessage createObjectMessage(Serializable object) throws JMSException;

Source Link

Document

Creates an initialized ObjectMessage object.

Usage

From source file:org.jbpm.ejb.impl.CommandListenerBean.java

private void sendResult(Serializable result, Destination destination, String correlationId)
        throws JMSException {
    log.debug("sending result " + result + " to " + destination);
    Session jmsSession = createSession();
    try {/*from   w  w  w. java2 s.  c  o  m*/
        Message resultMessage = jmsSession.createObjectMessage(result);
        resultMessage.setJMSCorrelationID(correlationId);
        jmsSession.createProducer(destination).send(resultMessage);
    } finally {
        jmsSession.close();
    }
}

From source file:org.marketcetera.client.jms.JMSMessageConverter.java

/**
 * Converts a trade message to a JMS Message.
 *
 * @param inObject the message to be converted. It should either be
 * an order or a report.//  w  ww  . j av a2 s .c  o m
 * @param session the JMS Session instance.
 *
 * @return the JMS message.
 *
 * @throws JMSException if there were errors serializing the
 * trade message.
 * @throws MessageConversionException if the supplied object was not
 * an acceptable trade message.
 */
@Override
public Message toMessage(Object inObject, Session session) throws JMSException, MessageConversionException {
    SLF4JLoggerProxy.debug(this, "Converting to JMS {}", inObject); //$NON-NLS-1$
    if (inObject instanceof TradeMessage) {
        return session.createObjectMessage((Serializable) inObject);
    } else {
        throw new MessageConversionException(
                new I18NBoundMessage1P(Messages.UNEXPECTED_MESSAGE_TO_SEND, ObjectUtils.toString(inObject))
                        .getText());
    }
}

From source file:org.marketcetera.jms.JMSSerMessageConverter.java

/**
 * Converts an object to a JMS Message by serializing it.
 *
 * @param inObject the object to be converted. The object should be
 * serializable./*from w w  w  .j a va  2 s  . c om*/
 * @param session the JMS Session instance.
 *
 * @return the JMS message.
 *
 * @throws JMSException if there were errors serializing the object.
 * @throws MessageConversionException if the supplied object was not
 * serializable or if there were errors serializing the object.
 */
@Override
public Message toMessage(Object inObject, Session session) throws JMSException, MessageConversionException {
    SLF4JLoggerProxy.debug(this, "Converting to JMS {}", inObject); //$NON-NLS-1$
    if (inObject instanceof Serializable) {
        try {
            return session.createObjectMessage((Serializable) inObject);
        } catch (RuntimeException e) {
            throw new MessageConversionException(
                    new I18NBoundMessage1P(Messages.UNEXPECTED_MESSAGE_TO_SEND, ObjectUtils.toString(inObject))
                            .getText(),
                    e);
        }
    } else {
        throw new MessageConversionException(
                new I18NBoundMessage1P(Messages.UNEXPECTED_MESSAGE_TO_SEND, ObjectUtils.toString(inObject))
                        .getText());
    }
}

From source file:org.rhq.enterprise.server.drift.DriftManagerBean.java

@Override
@TransactionAttribute(REQUIRES_NEW)/*from   www .  java  2  s .co  m*/
public void addChangeSet(Subject subject, int resourceId, long zipSize, InputStream zipStream)
        throws Exception {

    Connection connection = factory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(changesetQueue);
    ObjectMessage msg = session.createObjectMessage(new DriftUploadRequest(resourceId, zipSize, zipStream));
    producer.send(msg);
    connection.close();
}

From source file:org.rhq.enterprise.server.drift.DriftManagerBean.java

@Override
@TransactionAttribute(REQUIRES_NEW)//from   w  ww  .  ja v  a2  s.  c  o m
public void addFiles(Subject subject, int resourceId, String driftDefName, String token, long zipSize,
        InputStream zipStream) throws Exception {

    Connection connection = factory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(fileQueue);
    ObjectMessage msg = session
            .createObjectMessage(new DriftUploadRequest(resourceId, driftDefName, token, zipSize, zipStream));
    producer.send(msg);
    connection.close();
}

From source file:org.sakaiproject.kernel.messaging.activemq.ActiveMQEmailDeliveryT.java

public void testCommonsEmailOneWaySeparateSessions() {

    Queue emailQueue = null;/*from   w  ww. j ava  2s  .com*/
    MessageConsumer consumer = null;
    MessageProducer producer = null;
    Session clientSession = null;
    Session listenerSession = null;
    // it is not necessary to use the Email interface here
    // Email is used here just to allow for multiple types of emails to
    // occupy
    // the same varaible. SimpleEmail etc can each be used directly.
    List<Email> emails = new ArrayList<Email>();
    EmailMessagingService messagingService = new EmailMessagingService(vmURL, emailQueueName, emailType, null,
            null, null, null);
    emails.add(new SimpleEmail(messagingService));
    emails.add(new MultiPartEmail(messagingService));
    emails.add(new HtmlEmail(messagingService));
    try {

        listenerSession = listenerConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        emailQueue = listenerSession.createQueue(emailQueueName);

        consumer = listenerSession.createConsumer(emailQueue);

        consumer.setMessageListener(new EmailListener());

        listenerConn.start();

        listenerSession.run();

    } catch (JMSException e2) {
        e2.printStackTrace();
        Assert.assertTrue(false);
    }

    Wiser smtpServer = new Wiser();
    smtpServer.setPort(smtpTestPort);
    smtpServer.start();

    try {
        clientSession = clientConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        emailQueue = clientSession.createQueue(emailQueueName);
        producer = clientSession.createProducer(emailQueue);

        clientConn.start();
        clientSession.run();

    } catch (JMSException e) {
        e.printStackTrace();
        Assert.assertTrue(false);
    }

    for (Email em : emails) {

        try {
            em.addTo(TEST_EMAIL_TO);
            em.setFrom(TEST_EMAIL_FROM_ADDRESS, TEST_EMAIL_FROM_LABEL);
            // host and port will be ignored since the email session is
            // established
            // by
            // the listener
            em.setHostName("localhost");
            em.setSmtpPort(smtpTestPort);
            em.setSubject(TEST_EMAIL_SUBJECT);
            if (em instanceof HtmlEmail) {
                em.setMsg(TEST_EMAIL_BODY_HTMLEMAIL);
            } else if (em instanceof MultiPartEmail) {
                em.setMsg(TEST_EMAIL_BODY_MULTIPARTEMAIL);
            } else if (em instanceof SimpleEmail) {
                em.setMsg(TEST_EMAIL_BODY_SIMPLEEMAIL);
            }

        } catch (EmailException e1) {
            Assert.assertTrue(false);
            e1.printStackTrace();
        }

        try {
            em.buildMimeMessage();
        } catch (EmailException e1) {
            e1.printStackTrace();
            Assert.assertTrue(false);
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {

            em.getMimeMessage().writeTo(os);
        } catch (javax.mail.MessagingException e) {
            e.printStackTrace();
            Assert.assertTrue(false);
        } catch (IOException e) {
            e.printStackTrace();
            Assert.assertTrue(false);
        }

        String content = os.toString();

        ObjectMessage om;
        try {
            om = clientSession.createObjectMessage(content);

            om.setJMSType(emailType);

            LOG.info("Client: Sending test message....");
            producer.send(om);
        } catch (JMSException e) {
            e.printStackTrace();
            Assert.assertTrue(false);
        }

    }

    long start = System.currentTimeMillis();
    while (listenerMessagesProcessed < 3 && System.currentTimeMillis() - start < 10000L) {
        // wait for transport
    }
    Assert.assertTrue(listenerMessagesProcessed == 3);

    List<WiserMessage> messages = smtpServer.getMessages();
    Assert.assertTrue(messages.size() + " != expected value of 3", messages.size() == 3);

    for (WiserMessage wisermsg : messages) {
        String body = null;
        String subject = null;
        MimeMessage testmail = null;

        try {
            testmail = wisermsg.getMimeMessage();
        } catch (MessagingException e) {
            Assert.assertTrue(false);
            e.printStackTrace();
        }

        if (testmail != null) {
            LOG.info("SMTP server: test email received: ");
            try {
                LOG.info("To: " + testmail.getHeader("To", ","));

                LOG.info("Subject: " + testmail.getHeader("Subject", ","));
                body = getBodyAsString(testmail.getContent());
                subject = testmail.getHeader("Subject", ",");
            } catch (MessagingException e) {
                Assert.assertTrue(false);
                e.printStackTrace();
            } catch (IOException e) {
                Assert.assertTrue(false);
                e.printStackTrace();
            }
            LOG.info("Body: " + body);
            Assert.assertTrue(subject.contains(TEST_EMAIL_SUBJECT));
            Assert.assertTrue(body.contains("This is a Commons"));
        } else {
            Assert.assertTrue(false);
        }
    }

    if (clientSession != null) {
        try {
            clientSession.close();
        } catch (JMSException e) {
            e.printStackTrace();
            Assert.assertTrue(false);
        }
        clientSession = null;
    }

    if (listenerSession != null) {
        try {
            listenerSession.close();
        } catch (JMSException e) {
            e.printStackTrace();
            Assert.assertTrue(false);
        }
        listenerSession = null;
    }

    smtpServer.stop();

}

From source file:org.sakaiproject.kernel.messaging.email.EmailMessagingService.java

public String send(Email email) throws MessagingException, JMSException {
    try {//from   w ww  . j  a v  a 2s  . c  om
        email.buildMimeMessage();
    } catch (Exception e) {
        // this is a lossy cast. This would be a commons EmailException
        // this up cast is to keep commons-email out of our direct bindings
        throw new MessagingException(e);
    }

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        email.getMimeMessage().writeTo(os);
    } catch (javax.mail.MessagingException e) {
        throw new MessagingException(e);

    } catch (IOException e) {
        throw new MessagingException(e);
    }

    String content = os.toString();
    Connection conn = connectionFactory.createTopicConnection();
    conn.setClientID(getNextId());
    Session clientSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination emailTopic = clientSession.createTopic(emailQueueName);
    MessageProducer client = clientSession.createProducer(emailTopic);
    ObjectMessage mesg = clientSession.createObjectMessage(content);
    mesg.setJMSType(emailJmsType);
    client.send(mesg);
    // TODO finish this
    return null;

}

From source file:org.sakaiproject.kernel.messaging.JmsEmailMessageHandler.java

/**
 * {@inheritDoc}/*from  w  w  w.j a va 2 s  .com*/
 *
 * @see org.sakaiproject.kernel.api.messaging.MessageHandler#handle(java.lang.String,
 *      java.lang.String, java.lang.String, javax.jcr.Node)
 */
public void handle(String userID, String filePath, String fileName, Node node) {
    try {
        InputStream inputStream = nodeFactory.getInputStream(filePath);
        String content = IOUtils.readFully(inputStream, "UTF-8");
        Connection conn = connFactory.createConnection();
        conn.setClientID("sakai.emailmessagehandler");
        Session clientSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination emailTopic = clientSession.createTopic(emailQueueName);
        MessageProducer client = clientSession.createProducer(emailTopic);
        ObjectMessage mesg = clientSession.createObjectMessage(content);
        mesg.setJMSType(emailJmsType);
        client.send(mesg);
    } catch (JMSException e) {
        log.error(e.getMessage(), e);
    } catch (RepositoryException e) {
        log.error(e.getMessage(), e);
    } catch (JCRNodeFactoryServiceException e) {
        log.error(e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:org.sofun.core.messaging.SofunMessagingServiceImpl.java

@Override
public void sendMessage(Serializable message, String destination) {

    Connection connection = null;
    Session session = null;
    MessageProducer sender = null;/*from   ww w. ja v  a  2  s . c  om*/
    Queue q = getQueueFor(destination);
    if (q == null) {
        log.error("Cannot find associated queue for destination=" + destination);
    }
    try {
        connection = connFactory.createConnection(SofunMessagingCredentials.USERNAME,
                SofunMessagingCredentials.PASSWORD);
        session = connection.createSession(true, 0);
        sender = session.createProducer(q);
        ObjectMessage msg = session.createObjectMessage(message);
        sender.send(msg);
    } catch (Exception e) {
        log.error(e.getMessage());
    } finally {
        try {
            if (sender != null) {
                sender.close();
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (JMSException e) {
            log.error(e.getMessage());
        }

    }

}

From source file:ru.org.linux.search.SearchQueueSender.java

public void updateComment(final int msgid) {
    jmsTemplate.send(queue, new MessageCreator() {
        @Override//w ww  . j av a 2s  .c  o m
        public Message createMessage(Session session) throws JMSException {
            return session.createObjectMessage(new UpdateComments(Collections.singletonList(msgid)));
        }
    });
}