Sending Messages to Remote JMS Server

The below code will send Message to a Remotely Located JMS Server. package com.codefloor; import java.util.Properties; import javax.jms.ObjectMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.jms.JMSException; import javax.jms.Session; import javax.jms.QueueSession; import javax.jms.QueueSender; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.Queue; import javax.jms.DeliveryMode; import javax.naming.Context; import javax.naming.InitialContext; import javax.jms.TextMessage; public class MessageSender { private Queue _queue = null; private QueueConnection…

Details

Reading JMS Messages from Remote JMS Server

Below Java code will add a listener to the remote JMS Queue. package com.codefloor; import java.util.HashMap; import java.util.Properties; import javax.jms.TextMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.sql.Connection; import javax.jms.JMSException; import javax.jms.MessageListener; import javax.jms.QueueReceiver; import javax.jms.Session; import javax.jms.QueueSession; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.Queue; import javax.servlet.http.HttpServlet; import javax.naming.Context; import javax.naming.InitialContext; public class QueueListner extends HttpServlet implements MessageListener…

Details

check if string is palindrome in java using string buffer

package com.codefloor;
import java.util.*;
public class palindrome {
 public static void main(String[] args) {
 Scanner s=new Scanner(System.in);
 System.out.println("Enter the string to check for palindrome");
 String st1=s.nextLine();
 StringBuffer sb=new StringBuffer(st1);
 sb.reverse();
 if(st1.equals(sb.toString()))
 System.out.println("It is a Palindrome String");
 else
 System.out.println("Not a Palindrome String");
 }
}

Getting classpath from the class is loaded in java

package com.codefloor;

import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;

public class getClassPath{
public static void main(String[] args) {
try{
Class clsObj = Class.forName("com.codefloor.arrayList");
ProtectionDomain proDomain = clsObj.getProtectionDomain();
CodeSource codeSrc = proDomain.getCodeSource();
URL url = codeSrc.getLocation();
System.out.println("actual class path is " + url.getFile());
}catch(Exception ex){
ex.printStackTrace();
}
}
}
Output

actual class path is /C:/codefloor/working/com.codefloor/bin/

Creating Array List and Sorting it in Java

package com.codefloor; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class arrayList{ public static void main(String args[]){ List<String> nameList = new ArrayList<String>(); nameList.add(“Johny”); nameList.add(“Henry”); nameList.add(“Mark”); nameList.add(“Britto”); System.out.println(“Names in unsorted order”); for(String name: nameList){ System.out.println(name); } Collections.sort(nameList); System.out.println(“Names in sorted order”); for(String name: nameList){ System.out.println(name); } } } Output Names in unsorted order Johny Henry Mark Britto…

Details

Creating XML file using JAXB in Java

package com.codefloor; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement class xmlObject { String EmpName; int EmpAge; int EmpId; public String getEmpName() { return EmpName; } @XmlElement public void setEmpName(String EmpName) { this.EmpName = EmpName; } public int getEmpAge() { return EmpAge; } @XmlElement public void setEmpAge(int EmpAge) { this.EmpAge = EmpAge;…

Details

Reading a file in java through FileInputStream

package com.codefloor; import java.io.File; import java.io.FileInputStream; public class InputStreamExample { public static void main(String[] args) { File file = new File(“C:/samplefile.txt”); FileInputStream fis = null; try { fis = new FileInputStream(file); int data; System.out.print(“File Content is :”); while ((data = fis.read()) != -1) { System.out.print((char) data); } } catch (Exception e) { e.printStackTrace(); } finally…

Details