在TIBCO的情况下如何处理故障转移

时间:2018-07-28 05:06:12

标签: jms tibco tibco-ems

我正在努力在tibco JMS提供程序中设置故障转移。我知道在ActiveMQ的情况下该如何做。

我尝试的方法如下

    public class TibcoJMSQueueProducer {

    private static final Logger LOGGER = LoggerFactory.getLogger(FDPMetaCacheProducer.class);

    private static QueueConnectionFactory factory;
    private QueueConnection connection;
    private QueueSession session;

    @Inject
    private FDPTibcoConfigDAO fdpTibcoConfigDao;

    private String providerURL;

    private String userName;
    private String password;



    @PostConstruct
    public void constructProducer(){
        configure();
    }

    private void configure() {
        try {
            List<FDPTibcoConfigDTO> tibcoConfigList = fdpTibcoConfigDao.getAllTibcoConfig();
            if(!tibcoConfigList.isEmpty()){
                FDPTibcoConfigDTO fdpTibcoConfigDTO = tibcoConfigList.get(tibcoConfigList.size()-1);
                String providerURL = getProviderUrl(fdpTibcoConfigDTO);

                setProviderUrl(providerURL);
                String userName = fdpTibcoConfigDTO.getUserName();
                String password = fdpTibcoConfigDTO.getPassword();
                this.userName = userName;
                this.password=password;
                factory = new com.tibco.tibjms.TibjmsQueueConnectionFactory(providerURL);

            }

        } catch (Exception e) {
            System.err.println("Exitting with Error");
            e.printStackTrace();
            System.exit(0);
        }

    }

    private void setProviderUrl(String providerURL) {
        this.providerURL = providerURL;
    }

    private String getProviderUrl(final FDPTibcoConfigDTO FDPTibcoConfigDTO) {
        return TibcoConstant.TCP_PROTOCOL + FDPTibcoConfigDTO.getIpAddress().getValue() + TibcoConstant.COLON_SEPERATOR + FDPTibcoConfigDTO.getPort();
    }

    private Object lookupQueue(String queueName) {

        Properties props = new Properties();
        Object tibcoQueue = null;
        props.setProperty(Context.INITIAL_CONTEXT_FACTORY, TibcoConstant.TIB_JMS_INITIAL_CONTEXT_FACTORY);
        props.setProperty(Context.PROVIDER_URL, this.providerURL);
        props.setProperty(TibcoConstant.TIBCO_CONNECT_ATTEMPT, "20,10");
        props.setProperty(TibcoConstant.TIBCO_RECOVER_START_UP_ERROR, "true");
        props.setProperty(TibcoConstant.TIBCO_RECOVER_RECONNECT_ATTEMPT, "20,10");

        InitialContext context;
        try {
            context = new InitialContext(props);
            tibcoQueue = context.lookup(queueName);
        } catch (NamingException e) {
            System.out.println(e.getMessage());
        }

        return tibcoQueue;
    }

    public void pushIntoQueueAsync(String message,String queueName) throws JMSException {
        connection = factory.createQueueConnection(userName, password);
        connection.start();
        session = connection.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        Queue pushingQueue = (Queue)lookupQueue(queueName);
        QueueSender queueSender = session.createSender(pushingQueue);
        queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        TextMessage sendXMLRequest = session.createTextMessage(message);
        queueSender.send(sendXMLRequest);
        LOGGER.info("Pushing Queue {0} ,Pushing Message : {1}", pushingQueue.getQueueName(), sendXMLRequest.getText());
    }

    public String pushIntoQueueSync(String message,String queueName,String replyQueueName) throws JMSException {
        connection = factory.createQueueConnection(userName, password);
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = (Destination)lookupQueue(queueName);
        MessageProducer messageProducer = session.createProducer(destination);

        session = connection.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        UUID randomUUID =UUID.randomUUID();
       TextMessage textMessage = session.createTextMessage(message);
       String correlationId = randomUUID.toString();
       //Create Reply To Queue
       Destination replyDestination = (Destination)lookupQueue(queueName);
       textMessage.setJMSReplyTo(replyDestination);
       textMessage.setJMSCorrelationID(correlationId);
       String messgeSelector = "JMSCorrelationID = '" + correlationId + "'";

       MessageConsumer replyConsumer = session.createConsumer(replyDestination,messgeSelector);
       messageProducer.send(textMessage, javax.jms.DeliveryMode.PERSISTENT,   javax.jms.Message.DEFAULT_PRIORITY, 1800000);

       Message replayMessage = replyConsumer.receive();

       TextMessage replyTextMessage = (TextMessage) replayMessage;
       String replyText = replyTextMessage.getText();
       LOGGER.info("Pushing Queue {0} ,Pushing Message : {1}", queueName, message);
        return replyText;
    }

    public static QueueConnectionFactory getConnectionFactory(){
        return factory;
    }


}

在使用activeMQ的情况下,我们使用 failover:(tcp://127.0.0.1:61616,tcp://127.0.0.1:61616)?randomize=false&amp;backup=true URL用来作为ActiveMQconnectionfactory构造函数中的提供者URL处理故障转移。我已经看到在这样的情况下在某处使用多个URL tcp://169.144.87.25:7222,tcp://127.0.0.1:7222

我如何像这样检查故障转移。

首先,我使用单个IP(tcp://169.144.87.25:7222)进行了检查。消息正在正常发送和接收(我还没有发布TibcoJMSReceiver代码)。

我尝试使用另一个IP(tcp://169.144.87.25:7222)。一切正常。

但是当我尝试

  

最终字符串   PROVIDER_URL =“ tcp://169.144.87.25:7222,tcp://127.0.0.1:7222”;

我启动了程序。但是在提供输入之前,我先关闭了第一台服务器。作为故障转移,消息应发送到其他服务器。 但这向我显示了session closed Exception

那么我是否以正确的方式处理故障转移,还是我需要做其他配置?

1 个答案:

答案 0 :(得分:1)

如果在两个TIBCO EMS守护程序中都启用了容错功能,则它们只能“作为一个”工作。只有这样,他们才能彼此跳动并共享资源。您应该在远程守护程序的tibemsd.conf中找到它:

listen                  = tcp://7222
...
ft_active               = tcp://<ip to your box>:7222

,然后在您本地的盒子上

listen                  = tcp://7222
...
ft_active               = tcp://169.144.87.25:7222

您不需要每次都建立连接和会话!一个连接和多个消息会话-“容错”意味着它将自动为您重新连接。您可以调用一次一次的init()或connect()方法,也可以将其添加到您的configure方法中:

 private void configure() {
      try {
        ...
        connection = factory.createQueueConnection(userName, password);
        connection.start();
        session = connection.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);

然后pushIntoQueue变得如此简单:

public void pushIntoQueueAsync(String message,String queueName) throws JMSException {
    Queue pushingQueue = (Queue)lookupQueue(queueName);
    QueueSender queueSender = session.createSender(pushingQueue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    TextMessage sendXMLRequest = session.createTextMessage(message);
    queueSender.send(sendXMLRequest);
    LOGGER.info("Pushing Queue {0} ,Pushing Message : {1}", pushingQueue.getQueueName(), sendXMLRequest.getText());
}