Step 1: Create a simple maven project using you favorite IDE. I am using IntelliJ
Below I have mentioned dependencies required for the project. Add the dependency in your pom.xml
pom.xml
<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>
<groupId>com.dipenjms</groupId>
<artifactId>JMS</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
<name>JMS</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.jms/javax.jms-api -->
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Step 2:
Create a MyReceiver class which is a message consumer.
Note: java:comp/DefaultJMSConnectionFactory in initialContext.lookup() must match name Logical JNDI Name in server and also queue name.
package javatpoint.dipen.jms;
import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* Created by dipen on 5/5/2017.
*/
public class MyReceiver {
public static void main(String[] args) {
try {
InitialContext initialContext = new InitialContext();
QueueConnectionFactory queueConnectionFactory= (QueueConnectionFactory) initialContext.lookup("java:comp/DefaultJMSConnectionFactory");
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
queueConnection.start();
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) initialContext.lookup("myQueue");
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
final TextMessage[] textMessage = new TextMessage[1];
queueReceiver.setMessageListener( new MessageListener(){
@Override
public void onMessage(Message message) {
System.out.print("Inside Message Receiver" + message);
textMessage[0] = (TextMessage) message;
}
});
System.out.print(textMessage[0]);
queueConnection.close();
} catch (NamingException | JMSException e) {
e.printStackTrace();
}
}
}
Step 3:
Create a MySender class which is a message producer.
MySender.java
package javatpoint.dipen.jms;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.soap.Text;
import java.util.Properties;
/**
* Created by dipen on 5/5/2017.
*/
public class MySender {
public static void main(String[] args) {
Properties env = new Properties( );
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
/* env.put(Context.PROVIDER_URL,
"ts://localhost:4848");*/
try {
InitialContext initialContext = new InitialContext();
QueueConnectionFactory queueConnectionFactory= (QueueConnectionFactory) initialContext.lookup("java:comp/DefaultJMSConnectionFactory");
QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
queueConnection.start();
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) initialContext.lookup("myQueue");
QueueSender queueSender = queueSession.createSender(queue);
TextMessage textMessage = queueSession.createTextMessage();
textMessage.setText("Hello Dipen! This is JMS Testing");
queueSender.send(textMessage);
queueConnection.close();
} catch (NamingException | JMSException e) {
e.printStackTrace();
}
}
}
Step 4:
Install Glassfish4 Server from official site or Click Here and unzip in your desired directory.
Note: Download Java EE 7 Full Platform version.
Open bin directory from recently extracted Server directory and run asadmin.bat
For me it is: F:\IDE\Servers\glassfish4\bin
Then start the server using the command: start-domain
Now on your browser load the server admin page :http://localhost:4848/common/index.jsf
Step 5:
Click on the JMS Resource -> Connection Factories -> New, now write the pool name and select the Resource Type as QueueConnectionFactory then click on ok button.
Click on the JMS Resource -> Destination Resources -> New, now write the JNDI name and physical destination name then click on ok button.
Step 6: Run both MySender and MyReceiver.
Note : If there is error while running the project, add javaee.jar and appserv-rt.jar into your library from <yourpath>\glassfish\lib
For me it's: F:\IDE\Servers\glassfish4\glassfish\lib
Sender output
Receiver output
Download Source for GitHub : Click Here