The course is part of this learning path
In this course, we will learn the concepts of Java EE 7 with a focus on Java Message Service.
Learning Objectives
- Java Message Service (JMS)
Intended Audience
- Anyone looking to get Oracle Java Certification
- Those who want to improve Java 7 EE knowledge
- Java developers
Prerequisites
- Have at least 2 years of Java development experience
Hello dear friends. In this video, we will start to create a JMSProject to combine all we have learned. GlassFish 4 was installed on our Eclipse in the installation video. However, GlassFish 4.1 is also compatible with Java 7 EE and I'd like to use GlassFish 4.1 for my project. First, open any browser and perform a Google search for GlassFish 4.1. Go to the Oracle's GlassFish download page, select 4.1. Now in the release folder, download the GlassFish 4.1 zip file. Extract the zip to the C drive. Now open Eclipse. First, I'd like to configure my GlassFish 4.1 server in the server window. Right click on the server tab. New and server. In the GlassFish folder, select GlassFish. Next. With the browse button, we will select the GlassFish 4.1 path. In the java location, we have to select the JDK 7 folder. Next. Next and finish. Done. Now our server is ready. Let's create our project. File. New EJB Project. The name will be JMSMaster. This project will be our server project. Select target runtime GlassFish 4.1. Click 'Next', 'Next' again, and 'Finish'. Now we need a message driven bean. Right click on the project.
New, select a message driven bean project. The class name will be JmsMdb. Let's give a package. Let's define a destination name as Queue1. The destination type will be a queue. Next and finish. I want to delete the constructor. Let's create our client project. File, new project. Select Java project. Name will be JMSClient. JRE will be JDK 7. Next and finish. Click 'No'. Right click on the JMSClient project. Select properties. In the Java build path, select libraries tab. Click 'Add external jars'. Now we have to add a gf client jar. Open GlassFish, 4.1. GlassFish, lib and select the gf client jar. Open, click 'Apply' and 'Okay'. Now we need a new class named JmsMdbClient. Adjust a package. Finish. Now implement message listener interface. Import library. Now override the on message method. Now go back to the JmsMdb project. First, we need a try catch block. Inject JMSContext as we have learned in previous videos. We have to define our resources with resource annotation for Queue2. We have to import the queue library. It's important that we import the library from javaxJMS. Let's define a string message to hold the message body. Send message body from parameter.
Now let's show this message on the console. Now we have to call the createProducer method. Now we will determine our send message structure with the send method. We will send this message to the Queue2. Let's start the server. Right click on the server. Select GlassFish and go to view admin console. This will take a little time. In the left menu, find resources. Open JMS Resources. Click connection factories. Now we will define our factory. Click 'New'. Give a JNDI name. The resource type will be ConnectionFactory. Look, our factory is here. Let's go on with destination resources. Click 'New'. Now we have to define our queues. Queue01. Select resources type queue. Add. Let's add another queue named Queue02. Let's check the queues. Go back to the MdbClient. Now we need to specify initial context. Define the InitialContext() method, throws JMSException. As you remember, this exception is important for us. Add a naming exception also. Let's specify the properties. First, factory.initial. The second will be url.pkgs. The last one will be provide.url. Return InitialContext with new properties. Now it's the time to adjust the onMessage() method.
First, add try{}-catch() block. In try, we will show the received message on the console. Done. In the main method, first, we need JMSexception and NamingException. Now, let's start to use this JMS structure. First, create an instance from the MdbClient. Now, define a context with the MdbClient. Define queues. Queue01. Queue 02. Define JMSContext. Now, it's important that the name of the connection factory be the same as the name of the connection factory that we have defined in the admin view console. Now we need a consumer. Queue 02. The MessageListener will be the class's instance. We will define Producer with the create producer method of the JMSContext. If you want, we can give messages manually, but I want to get messages from users. Define a buffer. We need a string value to hold the user's message. Let's send a message to show that we are connected. In the consumer video, we have already learned a while loop is required. The condition will always be true, which means this condition will always work until the application is running. Get value from the user with a buffer. Let's define a finished method with the help of an if else condition.
If the message equals quit. Let's adjust case sensitivity with IgnoreCase. Close context. This will be our exit operation. Else. We will send the message to the queue01. Now that we have finished the structure, let's try. First, run the JMS MDB project. Now run the MdbClient project with the Java application. Look, now we are connected. Let's send some messages. And look, our message is here. Send another. Another. Now, let's try to quit. And look, our structure is ready. If you receive any certificate expiration errors, this indicates that the certificate's expiration date has passed. The problem will be resolved if you change the date on your computer back to 2016. This project is now complete. With this project, we have completed the JMS topic. In the following video, we will answer JMS topic questions. See you in the next video.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.