SHARE
TWEET

Untitled

a guest May 22nd, 2017 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * JBoss, Home of Professional Open Source
  3.  * Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual
  4.  * contributors by the @authors tag. See the copyright.txt in the
  5.  * distribution for a full listing of individual contributors.
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jboss.as.quickstarts.jms;
  18.  
  19. import domain.KweetJson;
  20.  
  21. import javax.jms.ConnectionFactory;
  22. import javax.jms.Destination;
  23. import javax.jms.JMSContext;
  24. import javax.naming.Context;
  25. import javax.naming.InitialContext;
  26. import javax.naming.NamingException;
  27. import java.util.Date;
  28. import java.util.Properties;
  29. import java.util.Scanner;
  30. import java.util.logging.Logger;
  31. import com.fasterxml.jackson.databind.JsonNode;
  32. import com.fasterxml.jackson.databind.ObjectMapper;
  33. import com.fasterxml.jackson.databind.node.JsonNodeFactory;
  34. import com.fasterxml.jackson.databind.node.ObjectNode;
  35.  
  36.  
  37. public class KwetterGo {
  38.     private static final Logger log = Logger.getLogger(KwetterGo.class.getName());
  39.  
  40.     // Set up all the default values
  41.     private static final String DEFAULT_MESSAGE = "Hello, World :)!";
  42.     private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
  43.     private static final String DEFAULT_DESTINATION = "jms/queue/kwetter";
  44.     private static final String DEFAULT_MESSAGE_COUNT = "5";
  45.     private static final String DEFAULT_USERNAME = "quickstartUser";
  46.     private static final String DEFAULT_PASSWORD = "quickstartPwd1!";
  47.     private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
  48.     private static final String PROVIDER_URL = "http-remoting://127.0.0.1:8080";
  49.  
  50.     public static void main(String[] args) {
  51.  
  52.         Context namingContext = null;
  53.  
  54.         try {
  55.             String userName = System.getProperty("username", DEFAULT_USERNAME);
  56.             String password = System.getProperty("password", DEFAULT_PASSWORD);
  57.  
  58.             // Set up the namingContext for the JNDI lookup
  59.             final Properties env = new Properties();
  60.             env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
  61.             env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
  62.             env.put(Context.SECURITY_PRINCIPAL, userName);
  63.             env.put(Context.SECURITY_CREDENTIALS, password);
  64.             namingContext = new InitialContext(env);
  65.  
  66.             // Perform the JNDI lookups
  67.             String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
  68.             log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
  69.             ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup(connectionFactoryString);
  70.             log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI");
  71.  
  72.             String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
  73.             log.info("Attempting to acquire destination \"" + destinationString + "\"");
  74.             Destination destination = (Destination) namingContext.lookup(destinationString);
  75.             log.info("Found destination \"" + destinationString + "\" in JNDI");
  76.  
  77.             int count = Integer.parseInt(System.getProperty("message.count", DEFAULT_MESSAGE_COUNT));
  78.             String content = System.getProperty("message.content", DEFAULT_MESSAGE);
  79.  
  80.             JsonNodeFactory factory = JsonNodeFactory.instance;
  81.             ObjectMapper mapper = new ObjectMapper();
  82.  
  83.             Scanner scanner = new Scanner(System.in);
  84.             while(true)
  85.             {
  86.                 System.out.println("KweeterId?");
  87.                 String kweeterId = scanner.nextLine();
  88.                 System.out.println("welkom: " + kweeterId);
  89.                 System.out.println("Message?");
  90.                 String kweet = scanner.nextLine();
  91.                 KweetJson kweetJson = new KweetJson(kweet, new Date(), Integer.parseInt(kweeterId));
  92.                 System.out.println(kweeterId + ": " + kweet);
  93. //                ObjectNode root = factory.objectNode();
  94.                 JsonNode kweetNode = mapper.convertValue(kweetJson, JsonNode.class);
  95. //                root.set("KweetJson", kweetNode);
  96.                 try (JMSContext context = connectionFactory.createContext(userName, password)) {
  97.                     log.info("Sending message as user " + kweeterId + " with content: " + kweetNode.toString());
  98.                     // Send the specified number of messages
  99.                     context.createProducer().send(destination, kweetNode.toString());
  100.                 }
  101.             }
  102.  
  103.         } catch (NamingException e) {
  104.             log.severe(e.getMessage());
  105.         } finally {
  106.             if (namingContext != null) {
  107.                 try {
  108.                     namingContext.close();
  109.                 } catch (NamingException e) {
  110.                     log.severe(e.getMessage());
  111.                 }
  112.             }
  113.         }
  114.     }
  115. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top