在安慰中需要一个示例Request(Java)-Reply(C ++)示例。

时间:2017-11-24 06:07:40

标签: java solace solace-mq

import com.solacesystems.jcsmp.BytesXMLMessage;
import com.solacesystems.jcsmp.JCSMPException;
import com.solacesystems.jcsmp.JCSMPFactory;
import com.solacesystems.jcsmp.JCSMPProperties;
import com.solacesystems.jcsmp.JCSMPRequestTimeoutException;
import com.solacesystems.jcsmp.JCSMPSession;
import com.solacesystems.jcsmp.JCSMPStreamingPublishEventHandler;
import com.solacesystems.jcsmp.Requestor;
import com.solacesystems.jcsmp.TextMessage;
import com.solacesystems.jcsmp.Topic;
import com.solacesystems.jcsmp.XMLMessageConsumer;
import com.solacesystems.jcsmp.XMLMessageListener;
import com.solacesystems.jcsmp.XMLMessageProducer;

public class REquestor {

public static void main(String... args) throws JCSMPException {
    // Check command line arguments
    String host="tcp://52.76.233.76:55555";
    String username="ccs_jcsmp_user_ccs3";
    String pwd="password";
    String vpn="default";

    System.out.println("BasicRequestor initializing...");

    // Create a JCSMP Session
    final JCSMPProperties properties = new JCSMPProperties();
    properties.setProperty(JCSMPProperties.HOST, host);     // host:port
    properties.setProperty(JCSMPProperties.USERNAME, username); // client-username
    properties.setProperty(JCSMPProperties.PASSWORD, pwd); // client-password
    properties.setProperty(JCSMPProperties.VPN_NAME,  vpn); // message-vpn
    final JCSMPSession session =  JCSMPFactory.onlyInstance().createSession(properties);
    session.connect();

    //This will have the session create the producer and consumer required
    //by the Requestor used below.

    /** Anonymous inner-class for handling publishing events */
    @SuppressWarnings("unused")
    XMLMessageProducer producer = session.getMessageProducer(new JCSMPStreamingPublishEventHandler() {

        public void responseReceived(String messageID) {
            System.out.println("Producer received response for msg: " + messageID);
        }

        public void handleError(String messageID, JCSMPException e, long timestamp) {
            System.out.printf("Producer received error for msg: %s@%s - %s%n",
                    messageID,timestamp,e);
        }
    });

    XMLMessageConsumer consumer = session.getMessageConsumer((XMLMessageListener)null);
//        final XMLMessageConsumer consumer = session.getMessageConsumer(new     XMLMessageListener() {
//
//            public void onReceive(BytesXMLMessage reply) {
//
//                System.out.printf("TextMessage reply received: '%s'%n",((TextMessage)reply).getText());
//
//            }
//
//            public void onException(JCSMPException e) {
//                System.out.printf("Consumer received exception: %s%n", e);
//            }
//        });
//        consumer.
    consumer.start();

    final Topic topic = JCSMPFactory.onlyInstance().createTopic("topicAnkit");

    //Time to wait for a reply before timing out
    final int timeoutMs = 100000;
    TextMessage request = JCSMPFactory.onlyInstance().createMessage(TextMessage.class);
    final String text = "Sample Request from Java!";
    request.setText(text);

    try {
        Requestor requestor = session.createRequestor();
        System.out.printf("Connected. About to send request message '%s' to topic '%s'...%n",text,topic.getName());
        BytesXMLMessage reply = requestor.request(request, timeoutMs, topic);

        // Process the reply
        if (reply instanceof TextMessage) {
            System.out.printf("TextMessage response received: '%s'%n",
                    ((TextMessage)reply).getText());
        }
        System.out.printf("Response Message Dump:%n%s%n",reply.dump());
    } catch (JCSMPRequestTimeoutException e) {
        System.out.println("Failed to receive a reply in " + timeoutMs + " msecs");
    }

    System.out.println("Exiting...");
    session.closeSession();
}
}

所以我的要求是我要向“TopicAnkit”发送消息并在某个主题/队列“topicResponse”中侦听响应。怎么做到这一点? 我在我的C ++ replier中看到我收到请求并将回复发送到此程序但是这个Java Requester没有收听任何主题。 我看到在sendTo字段中为请求者创建了一个临时主题,发送成功但请求者没有得到响应。 请指教 1.如何从这个临时主题中获取Java请求者的响应 2.如何指定监听主题,以便C ++可以将响应发送到该主题。

由于 ANKIT

2 个答案:

答案 0 :(得分:0)

您似乎没有明确设置要使用的回复主题。

可以在消息上设置,也可以在会话中设置,请参阅https://docs.solace.com/API-Developer-Online-Ref-Documentation/java/com/solacesystems/jcsmp/Requestor.html

添加

final Topic responseTopic = JCSMPFactory.onlyInstance().createTopic("responseTopic");

request.setReplyTo(responseTopic);

发送消息之前

答案 1 :(得分:0)

检查消息CorrelationId和ReplyTo。

根据安慰API documentation,请不要忘记“#”:

  

Requestor使用以'#'开头的CorrelationId值,这些值保留供内部使用,例如两方请求/回复。如果需要其他应用程序来接收答复,请指定一个应用程序CorrelationId。