Advertisement
Guest User

Untitled

a guest
Feb 1st, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. package jms.load;
  2.  
  3. import demo.app.DbHelper;
  4. import oracle.jms.AQjmsFactory;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.SpringApplication;
  9. import org.springframework.boot.autoconfigure.SpringBootApplication;
  10. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.context.annotation.Profile;
  14. import org.springframework.jms.annotation.EnableJms;
  15. import org.springframework.transaction.annotation.EnableTransactionManagement;
  16.  
  17. import javax.annotation.PostConstruct;
  18. import javax.jms.JMSException;
  19. import javax.jms.Queue;
  20. import javax.jms.QueueConnection;
  21. import javax.jms.QueueConnectionFactory;
  22. import javax.jms.QueueSender;
  23. import javax.jms.QueueSession;
  24. import javax.sql.DataSource;
  25. import java.sql.SQLException;
  26.  
  27. @SpringBootApplication
  28. @EnableTransactionManagement
  29. @EnableJms
  30. public class LoadGenerator {
  31.  
  32. private static final Logger LOG = LoggerFactory.getLogger(LoadGenerator.class);
  33.  
  34. private static final int load = 50000;
  35. private static final String queueName = "AQ_ADMIN.QUEUENAME";
  36. private static final String activeMqUrl = "tcp://localhost:61616";
  37. private static final String oracleAqJdbcUrl = "jdbc:oracle:thin:@hostname:1521:SID";
  38. private static final String oracleAqJdbcUser = "QUEUE_USER";
  39. private static final String oracleAqJdbcPassword = "QUEUE_PASSWORD";
  40. private static final String activeProfile = Profiles.OracleAQ;
  41.  
  42. @Autowired
  43. private QueueConnectionFactory queueConnectionFactory;
  44.  
  45. /**
  46. * Adds a message to the queue so the demo app can process it.
  47. *
  48. * @throws SQLException
  49. * @throws JMSException
  50. */
  51. @PostConstruct
  52. public void addMessageToQueue() throws SQLException, JMSException, InterruptedException {
  53. QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
  54. QueueSession queueSession = queueConnectionFactory.createQueueConnection().createQueueSession(true, QueueSession.AUTO_ACKNOWLEDGE);
  55. Queue queue = queueSession.createQueue(queueName);
  56. QueueSender queueSender = queueSession.createSender(queue);
  57.  
  58. LOG.info("Start sending {} messages", load);
  59.  
  60. for (int i = 0; i < load; i++) {
  61. queueSender.send(queueSession.createTextMessage("message"));
  62.  
  63. if (i % 1000 == 0) {
  64. LOG.info("progress... {}/{}...", i, load);
  65. queueSession.commit();
  66. }
  67. }
  68.  
  69. LOG.info("progress... {}/{}...", load, load);
  70. queueSession.commit();
  71.  
  72. LOG.info("Done.");
  73.  
  74. queueSender.close();
  75. queueSession.close();
  76. queueConnection.close();
  77.  
  78. }
  79.  
  80. public class Profiles {
  81. public static final String OracleAQ = "OracleAQ";
  82. public static final String ActiveMQ = "ActiveMQ";
  83. }
  84.  
  85. @Configuration
  86. @Profile(Profiles.OracleAQ)
  87. static class OracleAQ {
  88.  
  89. @Bean
  90. public DataSource dataSourceAq() throws SQLException {
  91. return DbHelper.createOracleDataSource(oracleAqJdbcUrl, oracleAqJdbcUser, oracleAqJdbcPassword);
  92. }
  93.  
  94. @Bean
  95. public QueueConnectionFactory queueConnectionFactory() throws SQLException, JMSException {
  96. return AQjmsFactory.getQueueConnectionFactory(dataSourceAq());
  97. }
  98. }
  99.  
  100. @Configuration
  101. @Profile(Profiles.ActiveMQ)
  102. static class ActiveMQ {
  103.  
  104. @Bean
  105. public QueueConnectionFactory queueConnectionFactory() throws SQLException, JMSException {
  106. return DbHelper.activeMqConnectionFactory(activeMqUrl);
  107. }
  108. }
  109.  
  110. public static void main(String[] args) {
  111. SpringApplication sa = new SpringApplication(LoadGenerator.class);
  112. sa.setAdditionalProfiles(activeProfile);
  113. sa.run(args);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement