Last active
June 2, 2018 16:32
-
-
Save soudmaijer/53fdc565ee05e4c08baf to your computer and use it in GitHub Desktop.
Small Spring Boot JMS queue load generator
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 jms.load; | |
import oracle.jdbc.pool.OracleDataSource; | |
import oracle.ucp.jdbc.PoolDataSource; | |
import oracle.ucp.jdbc.PoolDataSourceFactory; | |
import org.apache.activemq.ActiveMQConnectionFactory; | |
import org.apache.activemq.pool.PooledConnectionFactory; | |
import javax.jms.QueueConnectionFactory; | |
import javax.sql.DataSource; | |
import java.sql.SQLException; | |
import java.util.Properties; | |
public class DbHelper { | |
public static QueueConnectionFactory activeMqConnectionFactory(String activeMqUrl) { | |
ActiveMQConnectionFactory amcf = new ActiveMQConnectionFactory(activeMqUrl); | |
PooledConnectionFactory pcf = new PooledConnectionFactory(); | |
pcf.setConnectionFactory(amcf); | |
pcf.setMaxConnections(25); | |
return pcf; | |
} | |
public static DataSource createOracleUcpDataSource(String url, String username, String password) throws SQLException { | |
PoolDataSource poolDataSource = PoolDataSourceFactory.getPoolDataSource(); | |
poolDataSource.setConnectionFactoryClassName(OracleDataSource.class.getName()); | |
poolDataSource.setConnectionPoolName("ucp"); | |
poolDataSource.setUser(username); | |
poolDataSource.setPassword(password); | |
poolDataSource.setURL(url); | |
poolDataSource.setMaxPoolSize(15); | |
poolDataSource.setMinPoolSize(0); | |
poolDataSource.setMaxConnectionReuseCount(10); | |
poolDataSource.setMaxConnectionReuseTime(5L); | |
return poolDataSource; | |
} | |
public static DataSource createOracleDataSource(String url, String username, String password) throws SQLException { | |
final OracleDataSource oracleDataSource = new OracleDataSource(); | |
oracleDataSource.setURL(url); | |
oracleDataSource.setUser(username); | |
oracleDataSource.setPassword(password); | |
oracleDataSource.setLoginTimeout(10); | |
oracleDataSource.setExplicitCachingEnabled(true); | |
oracleDataSource.setConnectionCacheProperties(new Properties() {{ | |
put("MaxLimit", 100); | |
put("InitialLimit", 10); | |
}}); | |
return oracleDataSource; | |
} | |
} |
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 jms.load; | |
import oracle.jms.AQjmsFactory; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Profile; | |
import org.springframework.jms.annotation.EnableJms; | |
import org.springframework.transaction.annotation.EnableTransactionManagement; | |
import javax.annotation.PostConstruct; | |
import javax.jms.JMSException; | |
import javax.jms.Queue; | |
import javax.jms.QueueConnection; | |
import javax.jms.QueueConnectionFactory; | |
import javax.jms.QueueSender; | |
import javax.jms.QueueSession; | |
import javax.sql.DataSource; | |
import java.sql.SQLException; | |
@SpringBootApplication | |
@EnableTransactionManagement | |
@EnableJms | |
public class LoadGenerator { | |
private static final Logger LOG = LoggerFactory.getLogger(LoadGenerator.class); | |
private static final int load = 50000; | |
private static final String queueName = "AQ_ADMIN.QUEUENAME"; | |
private static final String activeMqUrl = "tcp://localhost:61616"; | |
private static final String oracleAqJdbcUrl = "jdbc:oracle:thin:@hostname:1521:SID"; | |
private static final String oracleAqJdbcUser = "QUEUE_USER"; | |
private static final String oracleAqJdbcPassword = "QUEUE_PASSWORD"; | |
private static final String activeProfile = Profiles.OracleAQ; | |
@Autowired | |
private QueueConnectionFactory queueConnectionFactory; | |
/** | |
* Adds a message to the queue so the demo app can process it. | |
* | |
* @throws SQLException | |
* @throws JMSException | |
*/ | |
@PostConstruct | |
public void addMessageToQueue() throws SQLException, JMSException, InterruptedException { | |
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); | |
QueueSession queueSession = queueConnectionFactory.createQueueConnection().createQueueSession(true, QueueSession.AUTO_ACKNOWLEDGE); | |
Queue queue = queueSession.createQueue(queueName); | |
QueueSender queueSender = queueSession.createSender(queue); | |
LOG.info("Start sending {} messages", load); | |
for (int i = 0; i < load; i++) { | |
queueSender.send(queueSession.createTextMessage("message")); | |
if (i % 1000 == 0) { | |
LOG.info("progress... {}/{}...", i, load); | |
queueSession.commit(); | |
} | |
} | |
LOG.info("progress... {}/{}...", load, load); | |
queueSession.commit(); | |
LOG.info("Done."); | |
queueSender.close(); | |
queueSession.close(); | |
queueConnection.close(); | |
} | |
public class Profiles { | |
public static final String OracleAQ = "OracleAQ"; | |
public static final String ActiveMQ = "ActiveMQ"; | |
} | |
@Configuration | |
@Profile(Profiles.OracleAQ) | |
static class OracleAQ { | |
@Bean | |
public DataSource dataSourceAq() throws SQLException { | |
return DbHelper.createOracleDataSource(oracleAqJdbcUrl, oracleAqJdbcUser, oracleAqJdbcPassword); | |
} | |
@Bean | |
public QueueConnectionFactory queueConnectionFactory() throws SQLException, JMSException { | |
return AQjmsFactory.getQueueConnectionFactory(dataSourceAq()); | |
} | |
} | |
@Configuration | |
@Profile(Profiles.ActiveMQ) | |
static class ActiveMQ { | |
@Bean | |
public QueueConnectionFactory queueConnectionFactory() throws SQLException, JMSException { | |
return DbHelper.activeMqConnectionFactory(activeMqUrl); | |
} | |
} | |
public static void main(String[] args) { | |
SpringApplication sa = new SpringApplication(LoadGenerator.class); | |
sa.setAdditionalProfiles(activeProfile); | |
sa.run(args); | |
} | |
} |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
.... | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.3.0.M5</version> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<java.version>1.8</java.version> | |
<spring.version>4.2.1.RELEASE</spring.version> | |
</properties> | |
<repositories> | |
<repository> | |
<id>spring-milestones</id> | |
<url>http://repo.spring.io/libs-milestone/</url> | |
</repository> | |
</repositories> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-jdbc</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-jms</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>com.oracle</groupId> | |
<artifactId>ojdbc6</artifactId> | |
<version>11.2.0.3</version> | |
</dependency> | |
<dependency> | |
<groupId>com.oracle</groupId> | |
<artifactId>ucp</artifactId> | |
<version>11.2.0.2.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.activemq</groupId> | |
<artifactId>activemq-all</artifactId> | |
<version>5.12.1</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.jms</groupId> | |
<artifactId>jms</artifactId> | |
<version>1.1</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.transaction</groupId> | |
<artifactId>jta</artifactId> | |
<version>1.1</version> | |
</dependency> | |
<dependency> | |
<groupId>oracle</groupId> | |
<artifactId>aqapi</artifactId> | |
<version>11.2.0.3</version> | |
</dependency> | |
</dependencies> | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment