Thursday, February 23, 2012

Struts2 example with Annotation. Struts 2 with annotation. First Struts 2 with annotation.


Struts2 First Example.


1. Create one Dynamic Project in Eclipse Say StrutsAnno
2. Put all these below jar file inside WEB-INF\lib folder
struts2-core-2.1.6.jar
xwork-2.1.2.jar
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-convention-plugin-2.1.6.jar

3. Change your web.xml (WEB-INF\web.xml) file as below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts2 First Example </display-name>
<filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>


There is no use of struts.xml if you use Annotation. :)

4. Create one package inside src folder say binod.suman
UserAction.java (StrutsAnno\src\binod\suman\UserAction.java)

package binod.suman;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class WelcomeUserAction {
private String userName;
private String message;

@Action(value="/welcome",results={@Result(name="success",location="/successPage.jsp"),@Result(name="error",location="/error.jsp")})
public String execute() {
message = "Welcome " + userName + " !";
System.out.println("Message : "+message);
if(userName.equals("Binod")){
 return "success";
}else{return "error";}
}

public void setUserName(String userName) {
this.userName = userName;
}

public void setMessage(String message) {
this.message = message;
}

public String getUserName() {
return userName;
}

public String getMessage() {
return message;
}
}


5. These below JSP page inside the WebContent
index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello World</title>
    </head>
    <body>
        <s:form action="welcome" >
            <s:textfield name="userName" label="User Name" />
            <s:submit />
        </s:form>
    </body>
</html>


error.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
  <title>Some Error</title>
  <link rel="stylesheet" href="mystyle.css" type="text/css" />
</head>
<body>
<h1>Error!</h1>
This error page is being shown because any of following reasons:
<ul class="boldred">
<li>Field(s) left blank.</li>
<li>Invalid Data Entered.(For example: String in place of Integer.)</li>
</ul>

<h1> You have entered <font color="red"> <b><s:property value="userName"/></b> </font> but you should suppose to enter <b>Binod</b> </h1>
</body>
</html>


successPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome User</title>
</head>
<body>
<b><s:property value="message"/></b>.
</body>
</html>


Only one web.xml, one java class and some jsp pages.

http://localhost:8080/StrutsAnno/

Put UserName Binod
Output:
Welcome Binod !.

That's it ............... :)

Thanks,

Binod Suman






First Example on Struts2, Struts2 Tutorial, Struts2 Easy Example


Why Struts2.


First Example on Struts2:

1. Create one Dynamic Project in Eclipse Say Struts2
2. Put all these below jar file inside WEB-INF\lib folder
struts2-core-2.0.6.jar
xwork-2.0.1.jar
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar

3. Change your web.xml (WEB-INF\web.xml) file as below:
web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts2 First Example </display-name>
<filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>



4. In Struts2/src folder, put these two file:
ClientAction.java

import com.opensymphony.xwork2.ActionSupport;

public class ClientAction extends ActionSupport{

String name;

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}

public String execute() throws Exception{
if(getName().equals("")){
      return ERROR;
        }
else{
return SUCCESS;
}
}

}



struts.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <include file="struts-default.xml"/>
  <package name="default" extends="struts-default">

<action name="clientAction" class="ClientAction">
<result name="success">/client.jsp</result>
<result name="error">/error.jsp</result>
<result name="input">/error.jsp</result>
</action>


   </package>
</struts>


5. Write some jsp file inside Struts2\WebContent
index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
  <s:form action="clientAction" method="post">
      <s:textfield label="Name" name="name"/>
    <s:submit/>
  </s:form>
 
  </body>
</html>


client.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
Thank you! <b><s:property value="name"/></b>.
<br><br>
</body>
</html>


error.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
  <title>Some Error</title>
  <link rel="stylesheet" href="mystyle.css" type="text/css" />
</head>
<body>
<h1>Error!</h1>
This error page is being shown because any of following reasons:
<ul class="boldred">
<li>Field(s) left blank.</li>
<li>Invalid Data Entered.(For example: String in place of Integer.)</li>
</ul>
</body>
</html>


That's it .........

Add any application Server inside the Eclipse and run .....
If you are getting some error that might be for update code.
So, In Eclipse bottom tab one Server tab is there -> locate your application server -> then right click on Struts1 (Project) -> click on "Clean Module Work Directory".


Now go to internet explorer and type URL:

http://localhost:8080/Struts2

and type your name in text box, you should suppose to get output like this

Thank you! Your Name.

Thanks,

Binod Suman





Struts2 why more powerful than Struts1. Struts2 Vs Struts1 . Compare between Struts1 and Struts2


STRUTS2 = STRUTS1 + Interceptor (=XWork) + OGNL - FormBean

1. Easy web.xml

STRUTS1:


<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
 
STRUTS2:

<filter>
<filter-name>webwork</filter-name>
<filter-class>
   org.apache.struts.action2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


No struts-config.xml inside WEB-INF folder.

2. Easy Action Class.

STRUTS1:
public class MyAction extends Action {
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws Exception {
        // do the work
        return (mapping.findForward("success"));
    }
}

All actions have to be thread-safe, as only a single action instance is created.
Struts 1 Actions are singletons and must be thread-safe since there will only be one instance of a class to handle all requests for that Action.
Because the actions have to be thread-safe, all the objects that may be needed in the processing of the action are passed in the method signature.

STRUTS2:
Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues.
public class MyAction {
   public String execute() throws Exception {
        // do the work
        return "success";
   }
}

Here no need to extends other class and no need to pass every thing in execute method.

If you want to use any session or request ro response object then use xxxxAware, example:
public class MyAction implements ServletRequestAware {
   private HttpServletRequest request;
   public void setServletRequest(HttpServletRequest request) {
        this.request = request;
   }
   public String execute() throws Exception {
        // do the work using the request
        return Action.SUCCESS;
   }
}

3. No Action From

STRUTS1:
Struts 1 uses an ActionForm object to capture input. Like Actions, all ActionForms must extend a base class. Since  other JavaBeans cannot be used as ActionForms, developers often create redundant classes to capture input. DynaBeans can used as an alternative to creating conventional ActionForm classes, but, here too, developers may be redescribing existing JavaBeans.

STRUTS2:
All data of ActionForm will do here in action class, so need to create one more class.The Action properties can be accessed from the web page via the taglibs.

4. Advanced Expression Language:

STRUTS1:
Struts 1 integrates with JSTL, so it uses the JSTL EL. The EL has basic object graph traversal, but relatively weak collection and indexed property support.

STRUTS2:
Struts 2 can use JSTL, but the framework also supports a more powerful and flexible expression language called "Object Graph Notation Language" (OGNL).

5. Easy Validation

STRUTS1:
Struts 1 supports manual validation via a validate method on the ActionForm, or through an extension to the Commons Validator.

STRUTS2:
Struts 2 supports manual validation via the validate method and the XWork Validation framework. The Xwork Validation Framework supports chaining validation into sub-properties using the validations defined for the properties class type and the validation context.

Content Source.
Another Source.

Thanks,

Binod Suman




Wednesday, February 22, 2012

Java Random number generation of 4 digits



import java.util.Random;

System.out.println("***** Generating Random Number of 4 digit *****");
Random random = new Random();
for(int i=0;i<100;i++){
    long fraction = (long)(1000 * random.nextDouble());
    int PIN= (int)(fraction + 1000);
    System.out.println(PIN);
 }

Wednesday, June 1, 2011

Spring RowMapper Example, Use of RowMapper, RowMapper Tutorial, jdbcTemplate Example with RowMapper

Interface RowMapper:

org.springframework.jdbc.core.RowMapper
An interface used by JdbcTemplate for mapping rows of a ResultSet on a per-row basis. Implementations of this interface perform the actual work of mapping each row to a result object. One very useful thing is that you can collect all the column of one recrod into java collection.

public class Student {
  private Map data = new HashMap();
  int roll;
}


Means, all the data will be there in map and only one primary column be there outside.

Example here:

Student.java

package binod.suman.rowmapper.domain;
import java.util.HashMap;
import java.util.Map;
public class Student {
 private Map data = new HashMap();

 int roll;

 public void putObject(String key, Object value) {
  data.put(key, value);
 }

 public Object getObject(String key) {
  return data.get(key);
 }

 public Student(int roll) {
  super();
  this.roll = roll;
 }

 public int getRoll() {
  return roll;
 }
 public void setRoll(int roll) {
  this.roll = roll;
 }
 @Override
 public String toString() {
  return "Name : "+data.get("sname")+" \nCity : "+data.get("city")+" \nRoll Number : "+roll;
 }


}

StudentResultSetReader.java

package binod.suman.rowmapper.dao;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import binod.suman.rowmapper.domain.Student;
public class StudentResultSetReader implements RowMapper {
 public StudentResultSetReader() {
 }

 public Student read(ResultSet rs) throws SQLException {
  Student t = new Student(rs.getInt("roll"));
  ResultSetMetaData md = rs.getMetaData();
  int numCols  = rs.getMetaData().getColumnCount();
  for (int i = 1; i <= numCols; i++) {
   t.putObject(md.getColumnName(i), rs.getObject(i));
  }
  return t;
 }
 @Override
 public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
  return read(rs);
 }
}

StudentDAO.java

package binod.suman.rowmapper.dao;

import java.util.List;
import binod.suman.rowmapper.domain.Student;
public interface StudentDAO {
// public void insertStudent(Student s);
 public Student selectStudent(int roll);
 public List selectAllStudent();
}


beanx.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans"
 http://www.springframework.org/schema/beans/spring-beans.xsd"/ >

 <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/suman"/>
        <property name="username" value="root"/>
        <property name="password" value="mysql"/>
    </bean>
 
 <bean id="studentDAO" class="binod.suman.rowmapper.dao.StudentDAOImpl">
  <property name="dataSource" ref="dataSource"/>
 </bean>

</beans>
Main.java

package binod.suman.rowmapper.dao;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import binod.suman.rowmapper.domain.Student;

public class Main {
 public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  StudentDAO studentDAO = (StudentDAO) context.getBean("studentDAO");
//  Student student = new Student(251,"Binod Suman", "Espoo");
//  studentDAO.insertStudent(student);
  Student ss = (Student)studentDAO.selectStudent(150);
  System.out.println(ss);
 
  List ssList = studentDAO.selectAllStudent();
  System.out.println("Total Record :: "+ssList.size());
  for(Student s : ssList){
   System.out.println("******************");
   System.out.println(s);
   }
  }
}


Jar files required:
org.springframework.asm-3.0.0.M3.jar
org.springframework.beans-3.0.0.M3.jar
org.springframework.context-3.0.0.M3.jar
org.springframework.context.support-3.0.0.M3.jar
org.springframework.core-3.0.0.M3.jar
org.springframework.expression-3.0.0.M3.jar
org.springframework.jdbc-3.0.0.M3.jar
org.springframework.transaction-3.0.0.M3.jar
mysql-connector-java-3.1.12-bin.jar
antlr-runtime-3.0.jar
commons-dbcp.jar
commons-logging-1.0.4.jar
commons-pool.jar
hsqldb.jar

You need to create one database schema with name suman and one student table shoule be there:

CREATE TABLE student (
  sname varchar(100) default NULL,
  roll int(4) NOT NULL,
  city varchar(100) default NULL,
  PRIMARY KEY  (`roll`)
)

and some data should be there:
insert into student ('Binod',150,'Helsinki');

Details documentation on RowMapper

Thanks,

Binod Suman

Tuesday, May 10, 2011

Spring Integration Messaging tutorial, Spring Integration in 10 Minutes


There are many things in Spring Integration:

1. Messaging
2. Routing
3. Mediation
4. Invocation
5. CEP (Complex Event Processing)
6. File Transfer
7. Shared database
8. Remote Procedure call

Here I am posting Spring Integration Messaging (Kind of JMS) example here:

Create one project in Eclipse say SpringIntegrationDemo and add these below jar file to that project:

1. spring-core-3.0.5.RELEASE.jar
2. spring-integration-core-2.0.0.BUILD-SNAPSHOT.jar
3. jar/commons-logging-1.1.jar
4. spring-context-3.0.5.RELEASE.jar
5. spring-beans-3.0.5.RELEASE.jar
6. spring-asm-3.0.5.RELEASE.jar
7. spring-expression-3.0.5.RELEASE.jar
8. spring-aop-3.0.5.RELEASE.jar
9. aopalliance-1.0.jar

In src folder, create these three files:
1. MyService.java
2. myServiceDemo.xml
3. MyServiceDemo.java

MyService.java

public class MyService {

 public String sayHello2(String name) {
  return "Suman Hello :  " + name;
  }

}

myServiceDemo.xml

<?xml version="1.0" encoding="UTF-8"?>
&it;beans:beans xmlns="http://www.springframework.org/schema/integration"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:beans="http://www.springframework.org/schema/beans"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/integration
   http://www.springframework.org/schema/integration/spring-integration.xsd" >

 <channel id="inputChannel"/>
 <channel id="outputChannel">
  <queue capacity="10"/>
 </channel>
 
 <service-activator input-channel="inputChannel"
                    output-channel="outputChannel"
                    ref="myService"
                    method="sayHello2"/>
 <beans:bean id="myService" class="MyService"/>
</beans:beans>

MyServiceDemo.java

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;
public class MyServiceDemo {

 public static void main(String[] args) {
  AbstractApplicationContext context = new ClassPathXmlApplicationContext("myServiceDemo.xml", MyServiceDemo.class);

  MessageChannel inputChannel =  context.getBean("inputChannel", MessageChannel.class);
  PollableChannel outputChannel =  context.getBean("outputChannel", PollableChannel.class);

// Just senging messages into message channel.
  for(int i=0;i<10;i++){
     inputChannel.send(new GenericMessage<String>("World : "+(i+1)));
  }
 
  // Getting message from message channel
  System.out.println("==> Returning from MyService : " + outputChannel.receive(0).getPayload());
  System.out.println("==> Returning from MyService : " + outputChannel.receive(0).getPayload());
  System.out.println("==> Returning from MyService : " + outputChannel.receive(0).getPayload());
  System.out.println("==> Returning from MyService : " + outputChannel.receive(0).getPayload());
  System.out.println("==> Returning from MyService : " + outputChannel.receive(0).getPayload());
 
 }
}

Now you can run MyServiceDemo file, will get result. :)



 

Saturday, May 7, 2011

How to implement MySql replication Master Master on same Windows machine


How to implement MySql replication Master Master on same windows machine.
In Master Master replication, the change effect to reflect vice varsa. If you change in one master it will effect automatically into other master also and vice varsa.

1. First install mysql-5.5.11-win32.msi on windows machine. While installing choose custom installation and change installation path to D:\MySQL\MySQL Server 5.5
2. Use all the default parameter like Service name is MYSQL and port number is 3306 and setup bin path.
3. And also modify root password to mysql.
3. Now check whether your installation is correct or not.
4.Open dos prompt and type below command.

c:\> mysql -uroot -pmysql -hlocalhost -P3306;
If you get mysql command then everything is ok.
We will treat mysql 5.5 is ACTIVE MASTER.
Now create one database:
mysql> create database suman;

5. Now install another version of mysql (mysql-5.1.56-win32.msi from http://dev.mysql.com/downloads/mysql/5.1.html)  for Passive MASTER. Again while installing choose custome installation and change installation path to D:\MySQL\MySQL Server 5.1

6. Change service name to MYSQL2 and port number to 3307
7. Modify root password to root.
8.Now check whether your installation is correct or not.
9.Open new dos prompot and type below command.

c:\> mysql -uroot -proot -hlocalhost -P3307;
If you get mysql command then everything is ok.
We will treat mysql 5.1 is PASSIVE MASTER.

Up to here two mysql instance are running in your windows machine.
Now start the replication implementation.

1. Open D:\MySQL\MySQL Server 5.5\my.ini then add four options to the [mysqld] section of the my.ini file

[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
server-id = 1
log_bin = mysql-bin.log
binlog_do_db = suman

save it.
Restart the MYSQL service from your pc. MyComputer -> Right click -> click on Manage -> Services and Application -> Services ->
search MYSQL on right side, right click on that MYSQL and click on restart.

The next step in setting up replication is creating an account that will be used exclusively for replication. We strongly advise creating a dedicated replication user be created for better security so you won't need to grant any additional privileges besides replication permissions. Create an account on the master server that the slave server can use to connect. As mentioned, this account must be given the REPLICATION SLAVE privilege.

Open one dos windows for all MASTER operation.
c:\>mysql -uroot -pmysql -hlocalhost -P3306;
mysql> create user 'user1' identified by 'password';

mysql> grant replication slave on *.* to 'user1'@'%'  identified by 'password';


mysql> flush privileges;
mysql> FLUSH TABLES WITH READ LOCK;
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |      107 | suman        |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
Please note down this file name and position, it will use to later.


Now some change on PASSIVE MASTER side:
1. Open D:\MySQL\MySQL Server 5.1\my.ini then add four options to the [mysqld] section of the my.ini file

[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3307
server-id = 2
log_bin = mysql-bin.log
binlog_do_db = suman

Restart the MYSQL2 service from your pc. MyComputer -> Right click -> click on Manage -> Services and Application -> Services ->
search MYSQL on right side, right click on that MYSQL2 and click on restart.
Open one dos windows for all SLAVE operation.
c:\>mysql -uroot -proot -hlocalhost -P3307;
mysql> stop slave;
mysql> CHANGE MASTER TO
MASTER_HOST='localhost
MASTER_USER='user1',
MASTER_PASSWORD='password',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=107;

Note: Values for the above command taken from Active Master 'show master status' command output.

mysql> show slave status\G;
Output will come huge, among two line should be like:
    Slave_IO_Running: No
    Slave_SQL_Running: No
   
Because slave is stopped now.
Now time came to start slave.
on slave side:
mysql> start slave;
Now check slave status:
mysql> show slave status\G;
Output will come huge, among two line should be like:
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes
   
 If both values are Yes, then everything are ok.

 Now create user on Passive Master side and give previleges.

mysql> GRANT REPLICATION SLAVE ON *.* TO 'user2'@'%' IDENTIFIED BY 'password';

mysql> show master status;
 +------------------+----------+--------------+------------------+
 | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
 +------------------+----------+--------------+------------------+
 | mysql-bin.000001 |      106 | suman        |                  |
 +------------------+----------+--------------+------------------+
 1 row in set (0.00 sec)

 Now come again on Active Master side (3306) console and type below command:
  
 mysql> stop slave;

 mysql> CHANGE MASTER TO
 MASTER_HOST='localhost',
 MASTER_USER='user2',
 MASTER_PASSWORD='password',
 MASTER_PORT=3307,
 MASTER_LOG_FILE='mysql-bin.000001',
 MASTER_LOG_POS=106;
Note: Values for the above command taken from Passive Master 'show master status' command output.


mysql> start slave;
mysql> show slave status\G;

 Now you can check your replication work.

 Create some table in MASTER suman database or any database then check at passive MASTER side. Now do some database operation
 at Passive MASTER side and check at Active MASTER side.
Thanks:

Binod Suman