Advertisement
Guest User

EmbeddedPublisher

a guest
Mar 31st, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. public class EmbeddedPublisher {
  2.  
  3.     public static void main(String... args) {
  4.         try {
  5.  
  6.             /**
  7.              * this map with configuration values is not necessary (it configures the default values).
  8.              * If you modify it to run the example in two different hosts, remember to also modify the
  9.              * server's Acceptor at {@link EmbeddedServer}
  10.              */
  11.             Map<String, Object> map = new HashMap<>();
  12.             map.put("host", "localhost");
  13.             map.put("port", 5445);
  14.             // -------------------------------------------------------
  15.  
  16.             ServerLocator serverLocator = HornetQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName(), map));
  17.             ClientSessionFactory sf = serverLocator.createSessionFactory();
  18.            
  19.             // Step 5. Create the session, and producer
  20.             try (ClientSession session = sf.createSession()) {
  21.  
  22.                 try (ClientProducer producer = session.createProducer(Properties.queueName)) {
  23.  
  24.                     // Step 6. Create and send a message
  25.                     ClientMessage message = session.createMessage(false);
  26.  
  27.                     //message.putStringProperty(Properties.propName, "Hello sent at " + new Date());
  28.  
  29.                     ObjectMapper mapper = new ObjectMapper();
  30.                     Map<String, Object> pojoMap = mapper.convertValue(new Pojo(13, "name", "phone"), new TypeReference<Map<String, Object>>() {});
  31.                     pojoMap.forEach(message::putObjectProperty);
  32.  
  33.                     System.out.println("Sending the message.. " + pojoMap.toString());
  34.  
  35.                     producer.send(message);
  36.                 }
  37.             }
  38.         } catch (Exception e) {
  39.             e.printStackTrace();
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement