Saturday, February 24, 2018

Topic Setup in RabbitMQ

1. Login into the RabbitMQ portal using guest credentials

http://localhost:15672/#/exchanges

2. Add a new exchange
  • Put Name jsa.topic select type as topic
  • Leave all other fields remain same and click on Add Exchange
3. Click on the newly created exchange jsa.topic
4. Go to the queue tab:

http://localhost:15672/#/queues

5.Click on Add Queues
  • Put Name: jsa.queue.1
  • Leave all other fields remain same and click on Add Queue
  • Put Name: jsa.queue.2
  • Leave all other fields remain same and click on Add Queue
  • Put Name: jsa.queue.3
  • Leave all other fields remain same and click on Add Queue
6. Click on the newly created queue jsa.queue.1
7.Go the binding tab in the jsa.queue.1


  • Put the From Exchange: jsa.topic
  • Leave other fields blank and click on bind button
  • Repeat the same for other queues.

Friday, February 23, 2018

Sending and Receiving Message from Queue in RabbitMQ Using Spring Boot

1. Create a New Maven Project(Create Simple Project)
2.Add the following dependency in the pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-amqp</artifactId>
        </dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.Add the following code to the application.properties
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
jsa.rabbitmq.exchange=jsa.direct
jsa.rabbitmq.routingkey=jsa.routingkey
jsa.rabbitmq.queue=jsa.queue

4. Create a Producer class

@Component
public class Producer {

@Autowired
private AmqpTemplate amqpTemplate;

@Value("${jsa.rabbitmq.exchange}")
private String exchange;

@Value("${jsa.rabbitmq.routingkey}")
private String routingKey;

public void produceMsg(String msg)
{
amqpTemplate.convertAndSend(exchange,routingKey,msg);
System.out.println("Send msg"+msg);
}
}

5.Create a mockup rest service to send the message
@RestController
public class WebController {

@Autowired
Producer producer;
@RequestMapping("/send")
public String sendMsg(@RequestParam("msg")String msg)
{
producer.produceMsg(msg);
return "Done";
}
}

6. Create a Consumer to consume the message
@Component
public class Consumer {
@RabbitListener(queues="${jsa.rabbitmq.queue}")
public void receivedMessage(String msg)
{
System.out.println("Recieved Msg"+msg);
}
}

Monday, February 19, 2018

Producer Consumer Queue Setup on RabbitMQ

1. Login into the RabbitMQ portal using guest credentials

http://localhost:15672/#/exchanges

2. Add a new exchange

  • Put Name jsa.direct
  • Leave all other fields remain same and click on Add Exchange
3. Click on the newly created exchange jsa.direct
4. Go to the queue tab:

http://localhost:15672/#/queues

5.Click on Add Queue
  • Put Name: jsa.queue
  • Leave all other fields remain same and click on Add Queue
6. Click on the newly created queue jsa.queue
7.Go the binding tab in the jsa.queue
  • Put the From Exchange: jsa.direct
  • Put the Routing Key: jsa.routingkey
  • Leave other fields blank and click on bind button


Installing RabbitMQ Server on Local Machine

  1. Install the Erlang(http://www.erlang.org/downloads)

2. Install the Rabbit MQ
https://www.rabbitmq.com

3. Open the Command Prompt
C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.3\sbin

4. Run the following command
rabbitmq-plugins.bat enable rabbitmq_management

5.Restart the RabbitMQ Services
6. Open browser
http://localhost:15672

7. Use guest as username and password

Thursday, January 4, 2018

Spring JPA Multiplicity Examples



References:



https://hellokoding.com/jpa-one-to-one-foreignkey-relationship-example-with-spring-boot-maven-and-mysql/

https://hellokoding.com/jpa-one-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/

https://hellokoding.com/jpa-many-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/

Sunday, December 10, 2017

Spring batch - running multiple jobs in parallel using PCF Scheduler

Step1: Download spring-boot-batch-multi-jobs project

https://github.com/making/spring-boot-batch-multi-jobs

Add following details to pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <scope>runtime</scope>
</dependency>

$ ./mvnw clean package -DskipTests=true
Run all jobs

$ java -jar target/spring-boot-batch-multi-jobs-0.0.1.RELEASE.jar
Run only specified jobs

Only job1

$ java -jar target/spring-boot-batch-multi-jobs-0.0.1.RELEASE.jar --spring.batch.job.names=job1
Only job2

$ java -jar target/spring-boot-batch-multi-jobs-0.0.1.RELEASE.jar --spring.batch.job.names=job2
job1 and job2

$ java -jar target/spring-boot-batch-multi-jobs-0.0.1.RELEASE.jar --spring.batch.job.names=job1,job2


References:

https://docs.spring.io/spring-batch/4.0.x/reference/html/domain.html#domainLanguageOfBatch
https://stackoverflow.com/questions/45718888/spring-batch-running-multiple-jobs-in-parallel
https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html
https://docs.spring.io/autorepo/docs/spring/4.1.4.RELEASE/javadoc-api/org/springframework/core/task/SimpleAsyncTaskExecutor.html
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-batch-applications.html



Saturday, December 9, 2017

a) Installing Mysql on Windows

1.Download Mysql zip(mysql-5.6.38-winx64) file

2. Extract the mysql folder

3. Execute the following commands
Step1: Run the server using the below command(C:\Softwares\mysql-5.6.38-winx64\bin)
Run mysqld --console

Step2: Reset the below command in new command prompt:(C:\Softwares\mysql-5.6.38-winx64\bin)

mysqladmin -u root password mysql

Step3: Connect mysql using the follwing command in new command prompt:(C:\Softwares\mysql-5.6.38-winx64\bin)

mysql -u root -p mysql


Step 4: Create database

create database db;

Step 5: Using the newly created db:

use db;


Step 6 : Shutdown of the Mysql
Run the server using the below command(C:\Softwares\mysql-5.6.38-winx64\bin)

Run mysqladmin -u root shutdown




b) Executing the mysql through mysql workbench:

1.Download Workbench from the below link:

https://dev.mysql.com/downloads/workbench/

2. Extract the file
3. Run the mysql work bench