Friday, December 28, 2012

MQ Message Post in Swing

/*Created by Sunil K Abraham ()
 This program accepts the queue details and takes the data from the specified location and puts the message into the queue*/
import javax.swing.*;
import java.awt.GridLayout;
import com.ibm.mq.*;
import java.io.*;
public class postMQmsg {
 public static void main(String[] args) {
  int countOfLines = 0;
  JPanel panel = new JPanel(new GridLayout(6, 0, 0, 0));
  JTextField field1 = new JTextField("WS95DEUE.OPR.TEST.cc");
  JTextField field2 = new JTextField("cc.SVRC");
  JTextField field3 = new JTextField("1414");
  JTextField field4 = new JTextField("WS95dewxDEUE");
  JTextField field5 = new JTextField("A1070.00.asdad");
  JTextField field6 = new JTextField(
    "C:\\AllShare\\DSS_Data_Generator\\Workings\\CA_Oct3\\GeneratedData1.txt");
  panel.add(new JLabel("Host Name:"));
  panel.add(field1);
  panel.add(new JLabel("Channel Name:"));
  panel.add(field2);
  panel.add(new JLabel("Port:"));
  panel.add(field3);
  panel.add(new JLabel("Queue Manager:"));
  panel.add(field4);
  panel.add(new JLabel("Queue:"));
  panel.add(field5);
  panel.add(new JLabel("Data File:"));
  panel.add(field6);
  String hostName, ChanelName, Port, queueMgr, queue, dataFile;
    
  hostName = field1.getText();
  ChanelName = field2.getText();
  Port = field3.getText();
  queueMgr = field4.getText();
  queue = field5.getText();
  dataFile = field6.getText();
  int option = JOptionPane.showConfirmDialog(null, panel,
    "MQ message posting - by TLQE", JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.PLAIN_MESSAGE);
  if (option == JOptionPane.OK_OPTION) {
   hostName = field1.getText();
   ChanelName = field2.getText();
   Port = field3.getText();
   queueMgr = field4.getText();
   queue = field5.getText();
   dataFile = field6.getText();
/*   System.out.println("hostName: " + hostName + "\n ChanelName: "
     + ChanelName + "\n Port: " + Port + "\n queueMgr: "
     + queueMgr + "\n queue: " + queue + "\n dataFile: "
     + dataFile);*/
   MQQueueManager QueueManager = null;
   MQPutMessageOptions PutMsgOptions;
   MQQueue QueueName = null;
   String sFilename = dataFile;
  
   int PortInt;
   PortInt = Integer.parseInt(Port);
   MQEnvironment.hostname = hostName;
   MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
   MQEnvironment.channel = ChanelName;
   MQEnvironment.port = PortInt;
   int openOptions = MQC.MQOO_OUTPUT;
   try {
    QueueManager = new MQQueueManager(queueMgr);
   } catch (MQException e) {
    e.printStackTrace();
    JPanel panelQM = new JPanel(new GridLayout(1, 0));
    JOptionPane.showMessageDialog(panelQM, "Queue Manager is incorrect", "TLQE - Warning", JOptionPane.WARNING_MESSAGE);
   }
   try {
    QueueName = QueueManager.accessQueue(queue, openOptions, null,
      null, null);
   } catch (MQException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    JPanel panelQN = new JPanel(new GridLayout(1, 0));
    JOptionPane.showMessageDialog(panelQN, "Queue Name is incorrect", "TLQE - Warning", JOptionPane.WARNING_MESSAGE);
   }
   PutMsgOptions = new MQPutMessageOptions();
   PutMsgOptions.options = MQC.MQPMO_DEFAULT_CONTEXT;
   try {
    FileInputStream fstream = new FileInputStream(sFilename);
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(
      new InputStreamReader(in));
    String strLine;
    while ((strLine = br.readLine()) != null) {
     if (strLine.length() > 10) {
      MQMessage myPutMessage = new MQMessage();
      myPutMessage.clearMessage();
      myPutMessage.persistence = MQC.MQPER_PERSISTENT;
      myPutMessage.format = MQC.MQFMT_STRING;
      myPutMessage.correlationId = MQC.MQCI_NONE;
      myPutMessage.messageId = MQC.MQMI_NONE;
      //System.out.println(strLine);
      myPutMessage.writeString(strLine);
      QueueName.put(myPutMessage, PutMsgOptions);
      countOfLines++;
     }
    }
    JPanel panel1 = new JPanel(new GridLayout(1, 0));
    JOptionPane.showMessageDialog(panel1, countOfLines
      + " messages posted to " + queue + " queue", "TLQE",
      JOptionPane.INFORMATION_MESSAGE);
   
    in.close();
    br.close();
    try {
     QueueName.close();
     QueueManager.disconnect();
    } catch (MQException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    JPanel panelFNF = new JPanel(new GridLayout(1, 0));
    JOptionPane.showMessageDialog(panelFNF, "Input Data File not Found", "TLQE - Warning", JOptionPane.WARNING_MESSAGE);
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    JPanel panelIOE = new JPanel(new GridLayout(1, 0));
    JOptionPane.showMessageDialog(panelIOE, "I/O Exception", "TLQE - Warning", JOptionPane.WARNING_MESSAGE);
    e.printStackTrace();
   } catch (MQException e) {
    // TODO Auto-generated catch block
     e.printStackTrace();
     JPanel panelMQExp = new JPanel(new GridLayout(1, 0));
     JOptionPane.showMessageDialog(panelMQExp, "Queue details are incorrect", "TLQE - Warning", JOptionPane.WARNING_MESSAGE);
   }  
  }
  if (option == JOptionPane.CANCEL_OPTION) {
   JPanel panelCancel = new JPanel(new GridLayout(1, 0));
   JOptionPane.showMessageDialog(panelCancel, "Bye Bye", "TLQE", JOptionPane.INFORMATION_MESSAGE);
  }
 }
}

Monday, December 10, 2012

Print a triangle



public class PrintTriangle {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
   int N = 10;
     for (int i = 0; i < N; i++) {
   // print j periods
   for (int j = 0; j < i; j++)
System.out.print("  ");
   // print N-i asterisks
   for (int j = 0; j < N-i; j++)
System.out.print("\\");
   // print N-i -
   for (int k = 0; k < N-i; k++)
System.out.print("-");
   // print N-i -
   for (int k = 0; k < N-i; k++)
System.out.print("-");
   // print N-i +
   for (int l = 0; l < N-i; l++)
System.out.print("/");
   // print a new line
   System.out.println();
}
}
}

Hello World!


/**
 *
 */

/**
 * @author tlqe
 *
 */
public class HelloWorld {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World from Eclipse");
}

}

Combine multiple text files into one


import java.io.*;

public class combineToSingleLine {
public static void main(String[] args) {
String dirPath = "C:\\Junk\\";
String outFile = dirPath + "out.txt";

File fileTemp = new File(outFile);
if (fileTemp.exists()) {
fileTemp.delete();
}

File dir_path = new File(dirPath);
File[] files = dir_path.listFiles();

try {
for (int i = 0; i < files.length; i++) {
String fileName = files[i] + "";
FileInputStream fstream = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(
new InputStreamReader(in));
String strLine;
FileWriter fOutstream = new FileWriter(outFile, true);
BufferedWriter out = new BufferedWriter(fOutstream);

while ((strLine = br.readLine()) != null) {
try {
System.out.println(strLine);
out.write(strLine);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
out.newLine();
in.close();
out.close();
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}

}
}

Delete all files from a directory


import java.io.File;


public class AllFilesDelete {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
   String dirPath="C:\\Junk\\"; //change the Directory accordingly
   File dir_path = new File(dirPath);
   File[] files = dir_path.listFiles();

   for(int i=0; i < files.length; i++){
   String fileName = files[i]+"";

File f1 = new File(fileName);
boolean success = f1.delete();

if (!success){
 System.out.println("Deletion failed.");
 System.exit(0);
}else{
 System.out.println(fileName + " - deleted.");
}
   }
 
if(files.length<1){
System.out.println("No files to be deleted");
}else
   System.out.println(files.length + " files deleted");
}
}