Search in sources :

Example 1 with Message

use of com.yanghui.elephant.common.message.Message in project elephant by yanghuijava.

the class RetrySendMQJob method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(ShardingContext shardingContext) {
    List<MessageEntity> findList = this.messageEntityMapper.querySendMQExcetion();
    log.info("查询消息确认但是发送mq失败的(查询1分钟之前的)记录数:{}", findList);
    if (findList.isEmpty()) {
        return;
    }
    for (MessageEntity find : findList) {
        Message message = new Message();
        message.setMessageId(find.getMessageId());
        message.setBody(find.getBody());
        message.setDestination(find.getDestination());
        if (!StringUtils.isEmpty(find.getProperties())) {
            message.setProperties(JSON.parseObject(find.getProperties(), Map.class));
        }
        this.producerService.sendMessage(message);
        MessageEntity update = new MessageEntity();
        update.setMessageId(find.getMessageId());
        update.setSendStatus(SendStatus.ALREADY_SEND.getStatus());
        update.setUpdateTime(new Date());
        this.messageEntityMapper.updateByMessageId(update);
    }
}
Also used : MessageEntity(com.yanghui.elephant.store.entity.MessageEntity) Message(com.yanghui.elephant.common.message.Message) Map(java.util.Map) Date(java.util.Date)

Example 2 with Message

use of com.yanghui.elephant.common.message.Message in project elephant by yanghuijava.

the class AbstractRequestProcessor method handleTransactionState.

@SuppressWarnings("unchecked")
protected void handleTransactionState(String messageId, String commitOrRollback) {
    LocalTransactionState localState = LocalTransactionState.valueOfName(commitOrRollback);
    MessageEntity findMessageEntity = this.messageEntityManager.findByMessageId(messageId);
    boolean sendMq = false;
    switch(localState) {
        case COMMIT_MESSAGE:
            this.messageEntityManager.updateStatusByMessageId(MessageStatus.CONFIRMED.getStatus(), findMessageEntity.getMessageId());
            sendMq = true;
            break;
        case ROLLBACK_MESSAGE:
            this.messageEntityManager.updateStatusByMessageId(MessageStatus.ROLLBACK.getStatus(), findMessageEntity.getMessageId());
            break;
        default:
            break;
    }
    if (sendMq) {
        Message message = new Message();
        message.setBody(findMessageEntity.getBody());
        message.setDestination(findMessageEntity.getDestination());
        message.setMessageId(findMessageEntity.getMessageId());
        if (!StringUtil.isEmpty(findMessageEntity.getProperties())) {
            message.setProperties((Map<String, String>) JSON.parseObject(findMessageEntity.getProperties(), Map.class));
        }
        this.producerService.sendMessage(message);
        this.messageEntityManager.updateSendStatusByMessageId(SendStatus.ALREADY_SEND.getStatus(), findMessageEntity.getMessageId());
    }
}
Also used : MessageEntity(com.yanghui.elephant.store.entity.MessageEntity) Message(com.yanghui.elephant.common.message.Message) LocalTransactionState(com.yanghui.elephant.common.constant.LocalTransactionState)

Example 3 with Message

use of com.yanghui.elephant.common.message.Message in project elephant by yanghuijava.

the class MessageRequestProcessor method normalMessageHandle.

@SuppressWarnings("unchecked")
private RemotingCommand normalMessageHandle(SendMessageRequestHeader requestHeader, RemotingCommand request) {
    RemotingCommand response = RemotingCommand.buildResposeCmd(ResponseCode.SERVER_FAIL, request.getUnique());
    MessageEntity entity = buildMessageEntity(request.getBody(), requestHeader, false);
    int responseCode = saveMessage(entity);
    if (responseCode != ResponseCode.SUCCESS) {
        response.setCode(responseCode);
        return response;
    }
    Message message = new Message();
    message.setBody(request.getBody());
    message.setDestination(requestHeader.getDestination());
    message.setMessageId(requestHeader.getMessageId());
    if (!StringUtil.isEmpty(requestHeader.getProperties())) {
        message.setProperties((Map<String, String>) JSON.parseObject(requestHeader.getProperties(), Map.class));
    }
    try {
        this.producerService.sendMessage(message);
    } catch (Exception e) {
        log.error("send mq exception:{}", e);
        response.setCode(ResponseCode.SEND_MQ_FAIL);
        return response;
    }
    response.setCode(ResponseCode.SUCCESS);
    try {
        this.messageEntityManager.updateSendStatusByMessageId(SendStatus.ALREADY_SEND.getStatus(), requestHeader.getMessageId());
    } catch (Exception e) {
        log.error("update message status exception:{}", e);
    }
    return response;
}
Also used : RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand) MessageEntity(com.yanghui.elephant.store.entity.MessageEntity) Message(com.yanghui.elephant.common.message.Message)

Example 4 with Message

use of com.yanghui.elephant.common.message.Message in project elephant by yanghuijava.

the class TransactionProducer method main.

public static void main(String[] args) throws MQClientException {
    TransactionMQProducer producer = new TransactionMQProducer("transaction-test");
    producer.setRegisterCenter("120.77.152.143:2181");
    producer.setTransactionCheckListener(new TransactionCheckListenerImpl());
    producer.start();
    LocalTransactionExecuter excuter = new LocalTransactionExecuterImpl();
    /**
     * 目前只支持activemq:
     * 发送queue,message的destination值加上前缀:queue://
     * 发送topic,message的destination值加上前缀:topic://
     */
    for (int i = 0; i < 1; i++) {
        Message msg = new Message("queue://yanghui.queue.test1", ("我是事务消息" + i).getBytes());
        TransactionSendResult transactionSendResult = producer.sendMessageTransaction(msg, excuter, null);
        System.out.println(transactionSendResult);
    }
// producer.shutdown();
}
Also used : Message(com.yanghui.elephant.common.message.Message) LocalTransactionExecuter(com.yanghui.elephant.client.producer.LocalTransactionExecuter) TransactionSendResult(com.yanghui.elephant.client.producer.TransactionSendResult) TransactionMQProducer(com.yanghui.elephant.client.producer.TransactionMQProducer)

Example 5 with Message

use of com.yanghui.elephant.common.message.Message in project elephant by yanghuijava.

the class TransactionProducer1 method main.

public static void main(String[] args) throws MQClientException {
    TransactionMQProducer producer1 = new TransactionMQProducer("transaction-test1");
    producer1.setRegisterCenter("172.16.21.12:2181");
    producer1.setTransactionCheckListener(new TransactionCheckListenerImpl());
    producer1.start();
    TransactionMQProducer producer2 = new TransactionMQProducer("transaction-test2");
    producer2.setRegisterCenter("172.16.21.12:2181");
    producer2.setTransactionCheckListener(new TransactionCheckListenerImpl2());
    producer2.start();
    LocalTransactionExecuter excuter = new LocalTransactionExecuterImpl();
    Message msg1 = new Message("topic://VirtualTopic.Test", ("我是事务消息1").getBytes());
    TransactionSendResult transactionSendResult1 = producer1.sendMessageTransaction(msg1, excuter, null);
    System.out.println("transactionSendResult1=" + transactionSendResult1);
    Message msg2 = new Message("topic://VirtualTopic.Test", ("我是事务消息2").getBytes());
    TransactionSendResult transactionSendResult2 = producer2.sendMessageTransaction(msg2, excuter, null);
    System.out.println("transactionSendResult2=" + transactionSendResult2);
}
Also used : Message(com.yanghui.elephant.common.message.Message) LocalTransactionExecuter(com.yanghui.elephant.client.producer.LocalTransactionExecuter) TransactionSendResult(com.yanghui.elephant.client.producer.TransactionSendResult) TransactionMQProducer(com.yanghui.elephant.client.producer.TransactionMQProducer)

Aggregations

Message (com.yanghui.elephant.common.message.Message)6 MessageEntity (com.yanghui.elephant.store.entity.MessageEntity)3 LocalTransactionExecuter (com.yanghui.elephant.client.producer.LocalTransactionExecuter)2 TransactionMQProducer (com.yanghui.elephant.client.producer.TransactionMQProducer)2 TransactionSendResult (com.yanghui.elephant.client.producer.TransactionSendResult)2 MQClientException (com.yanghui.elephant.client.exception.MQClientException)1 DefaultMQProducer (com.yanghui.elephant.client.producer.DefaultMQProducer)1 SendResult (com.yanghui.elephant.client.producer.SendResult)1 LocalTransactionState (com.yanghui.elephant.common.constant.LocalTransactionState)1 RemotingCommand (com.yanghui.elephant.remoting.procotol.RemotingCommand)1 Date (java.util.Date)1 Map (java.util.Map)1