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();
}
}
So my requirement is I am going to send message to "TopicAnkit" and listen for response in some topic/queue "topicResponse". How to achieve this? I see in my C++ replier I receive the request and send the reply to this program but this Java Requester is not listening to any topics. I see a temp topic is created in sendTo field to requester and sending is success but requester not getting the response. Please advice 1. How to get the response in Java requester from this temp topic 2. How to specify a listening topic so that C++ can send the response to this topic.
Thanks Ankit