Search in sources :

Example 1 with ActiveMQObjectMessage

use of org.apache.activemq.command.ActiveMQObjectMessage in project uavstack by uavorg.

the class CEMailAction method sendMail.

@Override
public boolean sendMail(String title, String mailTemplatePath, NotificationEvent notifyEvent) {
    if (log.isDebugEnable()) {
        log.debug(this, "Send Mail START: event=" + notifyEvent.toJSONString());
    }
    // ConnectionFactory :连接工厂,JMS 用它创建连接
    ActiveMQConnectionFactory connectionFactory;
    // Connection :JMS 客户端到JMS Provider 的连接
    Connection connection = null;
    // Session: 一个发送或接收消息的线程
    Session session = null;
    // Destination :消息的目的地;消息发送给谁.
    Queue destination;
    // MessageProducer:消息发送者
    javax.jms.MessageProducer producer = null;
    // TextMessage message;
    // 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
    connectionFactory = new ActiveMQConnectionFactory(userName, password, brokerURL);
    try {
        // 构造从工厂得到连接对象
        connection = connectionFactory.createConnection();
        // 启动
        connection.start();
        // 获取操作连接
        session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
        destination = session.createQueue(queueName);
        // 得到消息生成者【发送者】
        producer = session.createProducer(destination);
        // 设置不持久化,此处学习,实际根据项目决定
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        // 构造消息,此处写死,项目就是参数,或者方法获取
        EmailEvent emailEvent = buildEmailEvent(notifyEvent, title, mailTemplatePath);
        if (emailEvent == null) {
            return false;
        }
        // 序列化
        ActiveMQObjectMessage message = (ActiveMQObjectMessage) session.createObjectMessage();
        message.setObject(emailEvent);
        // 发送消息到目的地方
        producer.send(message);
        session.commit();
        return true;
    } catch (Exception e) {
        log.err(this, "Send Mail FAIL.", e);
    } finally {
        try {
            if (producer != null) {
                producer.close();
            }
        } catch (Throwable e) {
        // ignore
        }
        try {
            if (session != null) {
                session.close();
            }
        } catch (Throwable e) {
        // ignore
        }
        try {
            if (null != connection)
                connection.close();
        } catch (Throwable e) {
        // ignore
        }
    }
    return false;
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) EmailEvent(com.creditease.mspl.event.vo.EmailEvent) ActiveMQObjectMessage(org.apache.activemq.command.ActiveMQObjectMessage) Connection(javax.jms.Connection) Queue(javax.jms.Queue) Session(javax.jms.Session)

Example 2 with ActiveMQObjectMessage

use of org.apache.activemq.command.ActiveMQObjectMessage in project jim-framework by jiangmin168168.

the class AbstractConsumer method onMessage.

public void onMessage(Message message, Session session) throws JMSException {
    try {
        if (message == null) {
            log.info("received null data from {}", this.consumerConfig.getQueueName());
            return;
        }
        if (message instanceof ActiveMQObjectMessage) {
            Object object = ((ActiveMQObjectMessage) message).getObject();
            this.execute(object);
            this.commit(session);
        }
    } catch (Exception ex) {
        this.rollback(session);
        log.error("execute {} is failed:", this.consumerConfig.getQueueName(), ex);
        throw new JMSException(ex.getMessage());
    }
}
Also used : ActiveMQObjectMessage(org.apache.activemq.command.ActiveMQObjectMessage) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException)

Example 3 with ActiveMQObjectMessage

use of org.apache.activemq.command.ActiveMQObjectMessage in project activemq-artemis by apache.

the class OpenWireMessageConverter method toAMQMessage.

private static ActiveMQMessage toAMQMessage(MessageReference reference, ICoreMessage coreMessage, WireFormat marshaller, AMQConsumer consumer) throws IOException {
    final ActiveMQMessage amqMsg;
    final byte coreType = coreMessage.getType();
    final Boolean compressProp = (Boolean) coreMessage.getObjectProperty(AMQ_MSG_COMPRESSED);
    final boolean isCompressed = compressProp == null ? false : compressProp.booleanValue();
    final byte[] bytes;
    final ActiveMQBuffer buffer = coreMessage.getDataBuffer();
    buffer.resetReaderIndex();
    switch(coreType) {
        case org.apache.activemq.artemis.api.core.Message.BYTES_TYPE:
            amqMsg = new ActiveMQBytesMessage();
            bytes = toAMQMessageBytesType(buffer, isCompressed);
            break;
        case org.apache.activemq.artemis.api.core.Message.MAP_TYPE:
            amqMsg = new ActiveMQMapMessage();
            bytes = toAMQMessageMapType(buffer, isCompressed);
            break;
        case org.apache.activemq.artemis.api.core.Message.OBJECT_TYPE:
            amqMsg = new ActiveMQObjectMessage();
            bytes = toAMQMessageObjectType(buffer, isCompressed);
            break;
        case org.apache.activemq.artemis.api.core.Message.STREAM_TYPE:
            amqMsg = new ActiveMQStreamMessage();
            bytes = toAMQMessageStreamType(buffer, isCompressed);
            break;
        case org.apache.activemq.artemis.api.core.Message.TEXT_TYPE:
            amqMsg = new ActiveMQTextMessage();
            bytes = toAMQMessageTextType(buffer, isCompressed);
            break;
        case org.apache.activemq.artemis.api.core.Message.DEFAULT_TYPE:
            amqMsg = new ActiveMQMessage();
            bytes = toAMQMessageDefaultType(buffer, isCompressed);
            break;
        default:
            throw new IllegalStateException("Unknown message type: " + coreMessage.getType());
    }
    final String type = coreMessage.getStringProperty(JMS_TYPE_PROPERTY);
    if (type != null) {
        amqMsg.setJMSType(type);
    }
    amqMsg.setPersistent(coreMessage.isDurable());
    amqMsg.setExpiration(coreMessage.getExpiration());
    amqMsg.setPriority(coreMessage.getPriority());
    amqMsg.setTimestamp(coreMessage.getTimestamp());
    Long brokerInTime = (Long) coreMessage.getObjectProperty(AMQ_MSG_BROKER_IN_TIME);
    if (brokerInTime == null) {
        brokerInTime = 0L;
    }
    amqMsg.setBrokerInTime(brokerInTime);
    amqMsg.setCompressed(isCompressed);
    // we need check null because messages may come from other clients
    // and those amq specific attribute may not be set.
    Long arrival = (Long) coreMessage.getObjectProperty(AMQ_MSG_ARRIVAL);
    if (arrival == null) {
        // messages from other sources (like core client) may not set this prop
        arrival = 0L;
    }
    amqMsg.setArrival(arrival);
    final String brokerPath = (String) coreMessage.getObjectProperty(AMQ_MSG_BROKER_PATH);
    if (brokerPath != null && !brokerPath.isEmpty()) {
        setAMQMsgBrokerPath(amqMsg, brokerPath);
    }
    final String clusterPath = (String) coreMessage.getObjectProperty(AMQ_MSG_CLUSTER);
    if (clusterPath != null && !clusterPath.isEmpty()) {
        setAMQMsgClusterPath(amqMsg, clusterPath);
    }
    Integer commandId = (Integer) coreMessage.getObjectProperty(AMQ_MSG_COMMAND_ID);
    if (commandId == null) {
        commandId = -1;
    }
    amqMsg.setCommandId(commandId);
    final SimpleString corrId = (SimpleString) coreMessage.getObjectProperty(JMS_CORRELATION_ID_PROPERTY);
    if (corrId != null) {
        amqMsg.setCorrelationId(corrId.toString());
    }
    final byte[] dsBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_DATASTRUCTURE);
    if (dsBytes != null) {
        setAMQMsgDataStructure(amqMsg, marshaller, dsBytes);
    }
    final ActiveMQDestination actualDestination = consumer.getOpenwireDestination();
    amqMsg.setDestination(OpenWireUtil.toAMQAddress(coreMessage, actualDestination));
    final Object value = coreMessage.getGroupID();
    if (value != null) {
        String groupId = value.toString();
        amqMsg.setGroupID(groupId);
    }
    Integer groupSequence = (Integer) coreMessage.getObjectProperty(AMQ_MSG_GROUP_SEQUENCE);
    if (groupSequence == null) {
        groupSequence = -1;
    }
    amqMsg.setGroupSequence(groupSequence);
    final MessageId mid;
    final byte[] midBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_MESSAGE_ID);
    if (midBytes != null) {
        ByteSequence midSeq = new ByteSequence(midBytes);
        mid = (MessageId) marshaller.unmarshal(midSeq);
    } else {
        mid = new MessageId(UUIDGenerator.getInstance().generateStringUUID() + ":-1");
    }
    amqMsg.setMessageId(mid);
    final byte[] origDestBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_ORIG_DESTINATION);
    if (origDestBytes != null) {
        setAMQMsgOriginalDestination(amqMsg, marshaller, origDestBytes);
    }
    final byte[] origTxIdBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_ORIG_TXID);
    if (origTxIdBytes != null) {
        setAMQMsgOriginalTransactionId(amqMsg, marshaller, origTxIdBytes);
    }
    final byte[] producerIdBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_PRODUCER_ID);
    if (producerIdBytes != null) {
        ProducerId producerId = (ProducerId) marshaller.unmarshal(new ByteSequence(producerIdBytes));
        amqMsg.setProducerId(producerId);
    }
    final byte[] marshalledBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_MARSHALL_PROP);
    if (marshalledBytes != null) {
        amqMsg.setMarshalledProperties(new ByteSequence(marshalledBytes));
    }
    amqMsg.setRedeliveryCounter(reference.getDeliveryCount() - 1);
    final byte[] replyToBytes = (byte[]) coreMessage.getObjectProperty(AMQ_MSG_REPLY_TO);
    if (replyToBytes != null) {
        setAMQMsgReplyTo(amqMsg, marshaller, replyToBytes);
    }
    final String userId = (String) coreMessage.getObjectProperty(AMQ_MSG_USER_ID);
    if (userId != null) {
        amqMsg.setUserID(userId);
    }
    final Boolean isDroppable = (Boolean) coreMessage.getObjectProperty(AMQ_MSG_DROPPABLE);
    if (isDroppable != null) {
        amqMsg.setDroppable(isDroppable);
    }
    final SimpleString dlqCause = (SimpleString) coreMessage.getObjectProperty(AMQ_MSG_DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
    if (dlqCause != null) {
        setAMQMsgDlqDeliveryFailureCause(amqMsg, dlqCause);
    }
    final SimpleString lastValueProperty = coreMessage.getLastValueProperty();
    if (lastValueProperty != null) {
        setAMQMsgHdrLastValueName(amqMsg, lastValueProperty);
    }
    final Set<SimpleString> props = coreMessage.getPropertyNames();
    if (props != null) {
        setAMQMsgObjectProperties(amqMsg, coreMessage, props, consumer);
    }
    if (bytes != null) {
        ByteSequence content = new ByteSequence(bytes);
        amqMsg.setContent(content);
    }
    return amqMsg;
}
Also used : ActiveMQMapMessage(org.apache.activemq.command.ActiveMQMapMessage) ProducerId(org.apache.activemq.command.ProducerId) ActiveMQObjectMessage(org.apache.activemq.command.ActiveMQObjectMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQStreamMessage(org.apache.activemq.command.ActiveMQStreamMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) ActiveMQBytesMessage(org.apache.activemq.command.ActiveMQBytesMessage) ByteSequence(org.apache.activemq.util.ByteSequence) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) MessageId(org.apache.activemq.command.MessageId)

Example 4 with ActiveMQObjectMessage

use of org.apache.activemq.command.ActiveMQObjectMessage in project activemq-artemis by apache.

the class MessageTransformationTest method testTransformMessage.

/**
 * Tests transforming messages into ActiveMQ's message implementation.
 */
public void testTransformMessage() throws Exception {
    assertTrue("Transforming a BytesMessage message into an ActiveMQBytesMessage", ActiveMQMessageTransformation.transformMessage(new ActiveMQBytesMessage(), null) instanceof ActiveMQBytesMessage);
    assertTrue("Transforming a MapMessage message to an ActiveMQMapMessage", ActiveMQMessageTransformation.transformMessage(new ActiveMQMapMessage(), null) instanceof ActiveMQMapMessage);
    assertTrue("Transforming an ObjectMessage message to an ActiveMQObjectMessage", ActiveMQMessageTransformation.transformMessage(new ActiveMQObjectMessage(), null) instanceof ActiveMQObjectMessage);
    assertTrue("Transforming a StreamMessage message to an ActiveMQStreamMessage", ActiveMQMessageTransformation.transformMessage(new ActiveMQStreamMessage(), null) instanceof ActiveMQStreamMessage);
    assertTrue("Transforming a TextMessage message to an ActiveMQTextMessage", ActiveMQMessageTransformation.transformMessage(new ActiveMQTextMessage(), null) instanceof ActiveMQTextMessage);
    assertTrue("Transforming an ActiveMQMessage message to an ActiveMQMessage", ActiveMQMessageTransformation.transformMessage(new ActiveMQMessage(), null) instanceof ActiveMQMessage);
}
Also used : ActiveMQMapMessage(org.apache.activemq.command.ActiveMQMapMessage) ActiveMQBytesMessage(org.apache.activemq.command.ActiveMQBytesMessage) ActiveMQObjectMessage(org.apache.activemq.command.ActiveMQObjectMessage) ActiveMQStreamMessage(org.apache.activemq.command.ActiveMQStreamMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage)

Example 5 with ActiveMQObjectMessage

use of org.apache.activemq.command.ActiveMQObjectMessage in project activemq-artemis by apache.

the class CompressionOverNetworkTest method testObjectMessageCompression.

@Test
public void testObjectMessageCompression() throws Exception {
    MessageConsumer consumer1 = remoteSession.createConsumer(included);
    MessageProducer producer = localSession.createProducer(included);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    waitForConsumerRegistration(localBroker, 1, included);
    StringBuilder payload = new StringBuilder("test-");
    for (int i = 0; i < 100; ++i) {
        payload.append(UUID.randomUUID().toString());
    }
    Message test = localSession.createObjectMessage(payload.toString());
    producer.send(test);
    Message msg = consumer1.receive(RECEIVE_TIMEOUT_MILLS);
    assertNotNull(msg);
    ActiveMQObjectMessage message = (ActiveMQObjectMessage) msg;
    assertTrue(message.isCompressed());
    assertEquals(payload.toString(), message.getObject());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQBytesMessage(org.apache.activemq.command.ActiveMQBytesMessage) MapMessage(javax.jms.MapMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) ActiveMQObjectMessage(org.apache.activemq.command.ActiveMQObjectMessage) Message(javax.jms.Message) ActiveMQMapMessage(org.apache.activemq.command.ActiveMQMapMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) ActiveMQStreamMessage(org.apache.activemq.command.ActiveMQStreamMessage) ActiveMQObjectMessage(org.apache.activemq.command.ActiveMQObjectMessage) MessageProducer(javax.jms.MessageProducer) Test(org.junit.Test)

Aggregations

ActiveMQObjectMessage (org.apache.activemq.command.ActiveMQObjectMessage)11 Connection (javax.jms.Connection)3 MessageConsumer (javax.jms.MessageConsumer)3 MessageProducer (javax.jms.MessageProducer)3 Session (javax.jms.Session)3 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)3 ActiveMQBytesMessage (org.apache.activemq.command.ActiveMQBytesMessage)3 ActiveMQDestination (org.apache.activemq.command.ActiveMQDestination)3 ActiveMQMapMessage (org.apache.activemq.command.ActiveMQMapMessage)3 ActiveMQStreamMessage (org.apache.activemq.command.ActiveMQStreamMessage)3 ActiveMQTextMessage (org.apache.activemq.command.ActiveMQTextMessage)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ActiveMQMessage (org.apache.activemq.command.ActiveMQMessage)2 EmailEvent (com.creditease.mspl.event.vo.EmailEvent)1 Vector (java.util.Vector)1 BytesMessage (javax.jms.BytesMessage)1 JMSException (javax.jms.JMSException)1 MapMessage (javax.jms.MapMessage)1 Message (javax.jms.Message)1 Queue (javax.jms.Queue)1