Created
September 12, 2018 18:36
-
-
Save vladimir-ivanov/258b1971ecbb7a204bf0b16c3673988d to your computer and use it in GitHub Desktop.
solace example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vlad; | |
import com.solacesystems.jms.SolConnectionFactory; | |
import com.solacesystems.jms.SolJmsUtility; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.*; | |
import org.springframework.boot.autoconfigure.*; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.web.bind.annotation.*; | |
import javax.jms.*; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import java.util.concurrent.CountDownLatch; | |
@RestController | |
@SpringBootApplication | |
@EnableAutoConfiguration | |
@ComponentScan() | |
public class Example { | |
@Autowired | |
IntervalTicker intervalTicker; | |
@RequestMapping("/start") | |
String start() { | |
intervalTicker.start(); | |
return "Hello World!"; | |
} | |
@RequestMapping("/cancel") | |
String stop() { | |
intervalTicker.cancel(); | |
return "Hello World!"; | |
} | |
public static void main(String[] args) throws Exception { | |
SpringApplication.run(Example.class, args); | |
SolConnectionFactory connectionFactory = SolJmsUtility.createConnectionFactory(); | |
connectionFactory.setHost("tcp://vmr-mr8v6yiwid1f.messaging.solace.cloud:21216"); | |
connectionFactory.setUsername("solace-cloud-client"); | |
connectionFactory.setPassword("dui6httte9qt6r4jiapn0s2bth"); | |
connectionFactory.setVPN("msgvpn-85s7yc3aep"); | |
Connection connection = connectionFactory.createConnection(); | |
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | |
final String TOPIC_NAME = "vlad/topic"; | |
Topic topic = session.createTopic(TOPIC_NAME); | |
MessageConsumer messageConsumer = session.createConsumer(topic); | |
final CountDownLatch latch = new CountDownLatch(1); | |
messageConsumer.setMessageListener(new MessageListener() { | |
@Override | |
public void onMessage(Message message) { | |
try { | |
if (message instanceof TextMessage) { | |
System.out.printf("TextMessage received: '%s'%n", ((TextMessage) message).getText()); | |
} else { | |
System.out.println("Message received."); | |
} | |
System.out.printf("Message Content:%n%s%n", SolJmsUtility.dumpMessage(message)); | |
latch.countDown(); // unblock the main thread | |
} catch (JMSException ex) { | |
System.out.println("Error processing incoming message."); | |
ex.printStackTrace(); | |
} | |
} | |
}); | |
connection.start(); | |
latch.await(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment