Tomcat6 on Ubuntu, heap size and perm gen size

I’ve been having some trouble with a server running Tomcat6 on Ubuntu with a Spring/Hibernate set up. It also does some image resizing via Image Magick.

The problems were that the perm gen size for the Hibernate objects was too small and it ended up running out of memory.

No problem, in Ubuntu, the fix was going /etc/default/tomcat6 and editing the JAVA_OPTS variable.

I started off with

JAVA_OPTS="-XX:MaxPermSize=512m"

That solved the Hibernate problem but I was soon getting out of memory errors for image uploads. I had to add this into the file

JAVA_OPTS="-XX:MaxPermSize=512m -Xms256m -Xmx512m"

Running this gave me some useful feedback about the memory that Java was using.

jmap -heap <pid>

There are some useful resources out there for anyone interested in reading more

http://diegobenna.blogspot.co.uk/2011/02/how-to-increase-heap-size-in-tomcat-6.html

http://www.yaronco.com/tomcat6-and-java-heap-size/

PHP upload limits

Every now and then I need to re-set the PHP upload limits in the PHP ini file.

The obvious setting is

upload_max_filesize = 1024M

The less obvious settings are:

file_uploads = On
max_file_uploads=20
post_max_size=1024M

I’ve found it easy to forget to change post_max_size which need to be the same or greater than the upload_max_filesize.

Zend Framework, styling and decorating a form

Although extending Zend_Form produces a really easy form, the layout that results is less than perfect. This is where subclassing Zend_Form_Decorator_Abstract comes in. Here’s the implementation for a text field.

class Decorators_Inputs extends Zend_Form_Decorator_Abstract
{
    protected $_format = '
    <div class="element">
        <div class="label_col"
            <label for="%s">%s</label>
        </div>
         <div class="input_col">
            <input id="%s" name="%s" type="text" value="%s" class="%s" style="%s"/>
         </div>
         <div class="error_col">
            %s 
         </div>
     </div>
     <br clear="left" />
     ';

    public function render($content)
    {
        $element = $this->getElement();
        $name    = htmlentities($element->getFullyQualifiedName());
        $label   = htmlentities($element->getLabel());
        $id      = htmlentities($element->getId());
        $value   = htmlentities($element->getValue());
        $style   = htmlentities($element->getAttrib('style'));

        $class = "";
        $error = "";
        $errors = $element->getErrors();
        if($errors){
            $error = 'Please enter a value';
            $class = 'error';
        }

        $markup  = sprintf($this->_format, $name, $label, $id, $name, $value,$class, $style,$error);
        return $markup;
    }
}

Zend framework, sending an email

Sending an email in the Zend Framework turns out to be reasonably easy. This example uses the contact form for a web site

public function contactAction()
    {
        $request = $this->getRequest();
        $form = new Application_Form_Contact();

       //Check if form has been submitted
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())
              && $this->send($this->getRequest())) {

                $this->_forward('thanks');
            }
        }

        $this->view->form = $form;
    }

    private function send(Zend_Controller_Request_Abstract $request)
    {

        $session = new Zend_Session_Namespace();

        if (isset($session->numberOfPageRequests)) {
            $session->numberOfPageRequests++;
        } else {
            $session->numberOfPageRequests = 1;
        }

        //pretend mail has been sent for people repeatedly hitting refresh
        if ($session->numberOfPageRequests > 2) {
            return true;
        }

        $email_mapper = new Application_Model_EmailMapper();
        //Get the email text from the database.
        $email = $email_mapper->getEmailByName('ContactEmail', $request);

        $mail = new Zend_Mail();
        $mail->setBodyText($email->getText());
        $mail->setBodyHtml($email->getHtml());
        $mail->setFrom('somebody@example.com', 'Some Sender');
        $mail->addTo('somebody_else@example.com', 'Some Recipient');
        $mail->setSubject('TestSubject');
        $mail->send();

        return true;

    }

This is the result of sending the email

I’m using Toolheap’s Test email SMTP server to send test emails during development. It’s proven to be a very useful tool for verifying designs and content of the emails before they are put live.