Solace Topic Publisher Hello World
If you have created new or using existing Topic created by someone and for testing want to publish some test message on it. Please have simple Hello World program which will send message on Solace messaging server:
- SolaceTopicPublisherHelloWorld.java:
package com.javahonk.solace; import com.solacesystems.jcsmp.JCSMPException; import com.solacesystems.jcsmp.JCSMPFactory; import com.solacesystems.jcsmp.JCSMPProperties; import com.solacesystems.jcsmp.JCSMPSession; import com.solacesystems.jcsmp.JCSMPStreamingPublishEventHandler; import com.solacesystems.jcsmp.TextMessage; import com.solacesystems.jcsmp.Topic; import com.solacesystems.jcsmp.XMLMessageProducer; public class SolaceTopicPublisherHelloWorld { public static void main(String args[]) throws JCSMPException { JCSMPProperties properties = new JCSMPProperties(); properties.setProperty(JCSMPProperties.HOST, "HOST"); properties.setProperty(JCSMPProperties.VPN_NAME, "VPN"); properties.setProperty(JCSMPProperties.USERNAME, "USER_NAME"); properties.setProperty(JCSMPProperties.PASSWORD, "PASSWORD"); JCSMPSession session = JCSMPFactory.onlyInstance().createSession(properties); Topic topic = JCSMPFactory.onlyInstance().createTopic("TOPIC_NAME"); XMLMessageProducer publisher = session.getMessageProducer(new JCSMPStreamingPublishEventHandler() { public void responseReceived(String messageID) { System.out.println("Publisher received response for msg: " + messageID); } public void handleError(String messageID, JCSMPException e, long timestamp) { System.out.println("Publisher received error for msg:"+messageID+" TimeStamp: "+timestamp+" Exception details"+e); } }); TextMessage msg = JCSMPFactory.onlyInstance().createMessage(TextMessage.class); String text = "Solace hello world from Java Honk"; msg.setText(text); System.out.println("Connected to Solace topic sending message:"+text+" to the topic name: "+topic.getName()); publisher.send(msg,topic); System.out.println("Message sent successfully"); session.closeSession(); } }
Reference: