Monday, January 22, 2018

How to handle LocalDate/LocalTime in code of JDK 6

Question: I'm writing a library and it has some code like:
    public void foo(Object value){
      if(value instanceof Date){
          ...
      }else if(value instanceof Calendar){
          ...
      }...
    }
The code is supposed can be running on at lease JDK6. So, I have to code and compile on JRE6. But, in case, if a user works on JDK8 and passing a LocalDate/LocalTime object to the method.
How should I handle the case?
LocalDate/LocalTime can't compile on JRE6.
Or, I have to use two version of code? One for JDK6, and another for JDK8?
Answer:
1->Certainly you can't use your JDK 8 types in your JDK 6 code base. However, you may consider using JDK 6 types in your JDK 8 code base. For example, you could consider using 310-backports which basically provides the same set of classes in JDK 8 java.time API for applications running in earlier versions of Java. Unfortunately, this also means changing your JDK8-based APIs to use this backports. –
2->Here's a utility class I use to convert the newer java.time classes to java.util.Date objects and vice versa:
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class DateUtils {

  public static Date asDate(LocalDate localDate) {
    return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
  }

  public static Date asDate(LocalDateTime localDateTime) {
    return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
  }

  public static LocalDate asLocalDate(Date date) {
    return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
  }

  public static LocalDateTime asLocalDateTime(Date date) {
    return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
  }
}

How to disable the Tomcat Access log

The logging can be disabled in the following way:
  • Go to InstallationDirectoory/tomcat/conf.
  • Make a copy of server.xml file.
  • Change following line of the server.xml:(if user has Admin Permission then Okay, otherwise the current user has to get the modification permission) 

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false" /> 

    into: 

    <!--<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false" /> --> 
  • Restart Tomcat in order to make the change available.

Friday, January 19, 2018

remote-desktop-ubuntu-12-04-windows-7

Tomcat multiple instances simultaneously

Let's say that you have only one Tomcat folder located in C:\apache-tomcat-7.0.39, and that you wish to run two instances from it.
Make sure that you have CATALINA_HOME system/user variable set, and pointing to C:\apache-tomcat-7.0.39
  1. Create a folder C:\instance1. Copy confwebapps and temp folders from C:\apache-tomcat-7.0.39 and paste them to C:\instance1. You can delete contents from webapps and temp folders located under instance1, but don't touch conf contents.
  2. Now copy>paste C:\instance1 and rename it to instance2. That way, both instance1 and instance2 will have the same content.
  3. Go to C:\instance2\conf, edit server.xml and change the numbers of these ports (I marked those as XXXX):
    <Server port="XXXX" shutdown="SHUTDOWN">
    <Connector port="XXXX" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
    <Connector port="XXXX" protocol="AJP/1.3" redirectPort="8443" />
  4. Deploy whatever you want into instance1\webapps and instance2\webapps
  5. Create the following 4 batch files under C:\
instance1_startup.bat
@echo off
set CATALINA_BASE=C:\instance1
cd "%CATALINA_HOME%\bin"
set TITLE=My Tomcat Instance 01
call startup.bat %TITLE%
instance1_shutdown.bat
@echo off
set CATALINA_BASE=C:\instance1
cd "%CATALINA_HOME%\bin"
call shutdown.bat
instance2_startup.bat
@echo off
set CATALINA_BASE=C:\instance2
cd "%CATALINA_HOME%\bin"
set TITLE=My Tomcat Instance 02
call startup.bat %TITLE%
instance2_shutdown.bat
@echo off
set CATALINA_BASE=C:\instance2
cd "%CATALINA_HOME%\bin"
call shutdown.bat
  1. Run instance1_startup.bat and instance2_startup.bat, hopefully it should work.

Debugging Tomcat Remotely Using Eclipse

Skip to end of metadata
Go to start of metadata

Debugging Tomcat as a Remote External Application

Eclipse can be configured to provide debugging information for a running tomcat instance that is configured with JPDA support. This approach may work better for users using Windows.
Once you have tomcat configured:
  1. Start tomcat manually from the command line.
  2. Set a breakpoint somewhere in your code (preferably something not associated with startup) by left-clicking to the left of a line of code.
  3. Under the "Run" menu, select "Debug"
  4. Single-click the heading "Remote Java Application", then press the new button (looks like a page with a plus on it).
  5. On the dialog that appears, enter a meaningful name (like "Sakai (remote)" for this configuration).
  6. From the same dialog, change to the "Source" tab, then click "Add".
  7. On the dialog that appears, single-click "Java Project", then click "OK".
  8. On the dialog that appears, click "Select All" (or check the projects you wish to import), then click "OK".
  9. Now click the "Connect" tab and check the box marked "Allow termination of remote JVM".
  10. Click "Apply" to save your changes.
  11. Click "Debug" to start testing your configuration.
  12. Open your browser and work with Sakai until you reach your breakpoint. You should be directed into Eclipse where you will see the line of code with your breakpoint, a list of variables, etc., etc.

If you do not see the console output when using this method, you can simply keep open a separate window that displays the last contents of catalina.out.

Debugging Tomcat as an External Tool from within Eclipse

You can also configure Eclipse to be able to start and stop tomcat as a program (this approach also seems to work well on Windows). To configure Eclipse to be able to start and stop tomcat:

If you have already completed the above steps, you will need to stop tomcat to use the steps below.
  1. If you have not already done so, set a breakpoint somewhere in your code (preferably something not associated with startup) by left-clicking to the left of a line of code.
  2. Under the "Run" menu, select the "External Tools" sub-menu, then the "External Tools" heading.
  3. On the dialog that appears, single-click the "Program" heading, then click the "New" icon (a page with a plus on it).
  4. Change the Name to something meaningful, like "Sakai 2.4".
  5. Under "Location", enter full path and filename for your catalina script (catalina.sh on UNIX, catalina.bat on Windows).
  6. Under "Working Directory" to the location of your tomcat installation.
  7. Under "Arguments", enter "jpda start".
    • NOTE: If you use "jdpa run" rather than "jpda start", Tomcat will not open a new window to start in and will instead give you all it's standard err and standard out in the Eclipse console window. This is usefull for Windows developers who don't like to read console output from a DOS window.
  8. Go to the "Environment" tab.
  9. Click "New" and enter "JPDA_ADDRESS" as the name and "8000" as the value, then hit "OK".
  10. Click "New" again and enter "JPDA_TRANSPORT" as the name and "dt_socket" as the value, then hit "OK".
  11. Click "New" again and enter "JAVA_OPTS" as the name and "-server -XX:+UseParallelGC -Xmx768m -XX:MaxPermSize=160m -Djava.awt.headless=true" as the value, then hit "OK".
  12. Open the "Common" tab.
  13. Check the box marked "External Tools" in the panel marked "Display in favorites menu".
  14. Check the box marked "Launch in background".
  15. Hit "Apply" to save your changes.
  16. Hit "Run" to test your configuration.
  17. Once Sakai finishes starting up, open your browser and work with Sakai until you reach your breakpoint. You should be directed into Eclipse where you will see the line of code with your breakpoint, a list of variables, etc., etc.
Once you exit the debugger and stop tomcat, you should be able to start and stop tomcat with debugging enabled from within Eclipse by opening the "Run" menu and selecting "External Tools". You can also begin debugging your code by opening the "Run" menu and selecting "Debug".

tomcat will start when you run it as an external tool, but you will probably not see console output. You'll need to open another window that contains the contents of catalina.out to keep track of the startup or use jpda run

Configuring Tomcat as a Known Server from within Eclipse

You can also configure Eclipse to be able to start and stop tomcat as a server (this approach seems to work well on Unix). To configure Eclipse to be aware of tomcat as a server:

If you have already completed the above steps, you will need to stop tomcat to use the steps below.
  1. Edit ~/eclipse/eclipse.ini and change the memory settings as follows:
    -vmargs
    -Xms256m
    -Xmx1024m
    • NOTE: Eclipse has to have enough available memory to actually run Sakai within it
  2. If you have not already done so, set a breakpoint somewhere in your code (preferably something not associated with startup) by left-clicking to the left of a line of code.
  3. Configure Eclipse to launch the desired JVM with the appropriate memory settings:
    1. Under the "Eclipse" menu, select "Preferences"
    2. Open the "Java" heading and the "Installed JREs" subheading.
    3. Single-click the desired run-time, then click "Edit".
    4. Under "Default VM Arguments", enter "-server -XX:+UseParallelGC -Xmx1024m -XX:MaxPermSize=384m -Djava.awt.headless=true", then click "OK".
    5. Click "OK" again to close the preferences dialog.
  4. Add the Sakai tomcat instance to the list of known runtimes:
    1. Under the "Eclipse" menu, select "Preferences"
    2. Expand the "Server" heading on the left side of the dialog that appears, then single-click "Installed Runtimes" and click "Add".
    3. On the dialog that appears, expand the Apache heading, then single-click "Apache Tomcat v5.5" and click "Next".\
    4. Under "Name", enter something meaningful like "Sakai 2.4 tomcat".
    5. Under "Tomcat installation directory", enter the location of your tomcat home directory.
    6. Under "JRE", select the JVM you configured above.
    7. Click "Finish".
  5. Add the server to the debug view
    1. Open the "Debug" view.
    2. Open the "Servers" tab.
    3. Using the right mouse button (or control-click on the mac), open the dialog to create a new server.
    4. Under "Server's host name", fill in "localhost" if it doesn't already appear.
    5. Open the "Apache" heading, then select "Tomcat v5.5 Server".
    6. Under "Server Runtime", select the runtime you defined earlier.
    7. Click "Finish" (you're not actually finished (smile) )
    8. Double-click the new server entry, a dialog should appear with more advanced options.
    9. Change "Server name" to something meaningful like "Sakai 2.4 (Server)".
    10. Uncheck "Run modules directly from the workspace".
    11. Click "Open launch configuration".
    12. On the dialog that appears, open the "Arguments" tab. Change "program arguments" to "jpda start".
      • NOTE: If you use "jdpa run" rather than "jpda start", Tomcat will not open a new window to start in and will instead give you all it's standard err and standard out in the Eclipse console window. This is usefull for Windows developers who don't like to read console output from a DOS window.
    13. On the same dialog, change "VM arguments" so that catalina.base is set to the tomcat home directory.
    14. Open the "Source" tab, then click "Add".
    15. On the dialog that appears, single-click "Java Project", then click "OK".
    16. On the dialog that appears, click "Select All", then click "OK".
    17. Open the "Environment" tab, then click "New".
    18. Enter "JPDA_ADDRESS" for "Name", and "8000" for "Value", then click "OK".
    19. Click "Add" again, then enter "JPDA_TRANSPORT" for "Name" and "dt_socket" for "Value". Click "OK".
    20. Click "Apply", then "OK" to save your launch settings.
    21. IMPORTANT: Under the "File" menu, select "Save" to save your overall server definition.
    22. You should now be able to start up the server by right clicking (or control clicking) its heading in the "Servers" tab and selecting "Start".
    23. Once Sakai finishes starting up, open your browser and work with Sakai until you reach your breakpoint. You should be directed into Eclipse where you will see the line of code with your breakpoint, a list of variables, etc., etc.

Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...