Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. public class MyAuthenticationPlugin extends SimpleAuthenticationPlugin {
  2. private String username ="username";
  3. private String password ="password";
  4.  
  5. public MyAuthenticationPlugin(){
  6. secureME();
  7. }
  8. public void secureME(){
  9. Map<String, String> map = new HashMap<String, String>();
  10. map.put(username, password);
  11. this.setUserPasswords(map);
  12. }
  13. }
  14.  
  15. public class Server{
  16. private static int ackMode;
  17. private static String messageQueueName;
  18. private static String messageBrokerUrl;
  19.  
  20. private Session session;
  21. private boolean transacted = false;
  22. private MessageProducer replyProducer;
  23. private MessageProtocol messageProtocol;
  24. private String username ="username";
  25. private String password ="password";
  26.  
  27. static {
  28. messageBrokerUrl = "tcp://localhost:61616";
  29. messageQueueName = "client.messages";
  30. ackMode = Session.AUTO_ACKNOWLEDGE;
  31. }
  32.  
  33. public Server() {
  34. try {
  35. //This message broker is embedded
  36. BrokerService broker = new BrokerService();
  37. broker.setPersistent(false);
  38. broker.setUseJmx(false);
  39. broker.addConnector(messageBrokerUrl);
  40. // here I'm add my class as plguing
  41. MyAuthenticationPlugin[] myAuthenticationPlugin = new
  42. MyAuthenticationPlugin[1];
  43. myAuthenticationPlugin[0] = new MyAuthenticationPlugin();
  44.  
  45. broker.setPlugins(myAuthenticationPlugin);
  46. broker.start();
  47. } catch (Exception e) {
  48. System.out.println("Exception: "+e.getMessage());
  49. //Handle the exception appropriately
  50. }
  51. }
  52.  
  53. private void setupMessageQueueConsumer() {
  54. ActiveMQConnectionFactory connectionFactory = new
  55. ActiveMQConnectionFactory(messageBrokerUrl);
  56.  
  57. connectionFactory.setUserName(username);
  58. connectionFactory.setPassword(password);
  59.  
  60. Connection connection;
  61. try {
  62. connection = connectionFactory.createConnection(username, password);
  63.  
  64.  
  65. connection.start(); // This line thows exception
  66. this.session = connection.createSession(this.transacted, ackMode);
  67. Destination adminQueue = this.session.createQueue(messageQueueName);
  68.  
  69. //Setup a message producer to respond to messages from clients, we will get the destination
  70. //to send to from the JMSReplyTo header field from a Message
  71. this.replyProducer = this.session.createProducer(null);
  72. this.replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  73.  
  74. //Set up a consumer to consume messages off of the admin queue
  75. MessageConsumer consumer = this.session.createConsumer(adminQueue);
  76. consumer.setMessageListener(this);
  77.  
  78. // new BufferedReader(new InputStreamReader(System.in)).readLine();
  79.  
  80. } catch (JMSException e) {
  81. System.out.println("Exception: "+e.getMessage());
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. }
  86.  
  87. public static void main(String[] args) {
  88. new Server();
  89. System.out.println("I'm done. END");
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement