-2
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++ 回复器中看到我收到了请求并将回复发送到该程序,但是这个 Java 请求者没有收听任何主题。我看到在 sendTo 字段中向请求者创建了一个临时主题,并且发送成功,但请求者没有得到响应。请指教 1. 如何从这个临时主题中获取 Java 请求者的响应 2. 如何指定一个监听主题,以便 C++ 可以发送对该主题的响应。

感谢 Ankit

4

2 回答 2

0

检查消息 CorrelationId 和 ReplyTo。

根据 solace API 文档,不要忘记“#”:

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

于 2018-07-27T11:07:06.677 回答
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);

在发送消息之前

于 2017-12-12T13:55:12.793 回答