Table Of ContentEarly praise for Deploying with JRuby 9K
Joe has pulled together a great collection of deployment knowledge from his years
of experience building and supporting JRuby applications. He’s an expert on this
subject and Deploying with JRuby 9k is the definitive text for getting JRuby appli-
cations up and running.
➤ Charles Oliver Nutter
JRuby co-lead
Deploying with JRuby 9k answers the most frequently asked questions about real-
world use of JRuby. Whether you’re coming to JRuby from Ruby or Java, Joe fills
in all the gaps you’ll need to deploy JRuby with confidence.
➤ Tom Enebo
JRuby co-lead
I’ve been working with JRuby for years and I still learned several immediately
actionable steps to improve the performance and maintenance of real-world
JRuby apps.
➤ Matt Margolis
director, application development at Getty Images
Deploying with JRuby 9k is full of practical and actionable advice about how to
get the most benefit out of the JVM when running your Ruby app on JRuby.
➤ Chris Seaton
Oracle Labs and JRuby contributor
Deploying with JRuby 9k is the essential guide for anyone building Ruby applica-
tions on the JVM. It’s loaded with tips, tricks, and best practices that newcomers
and experts can learn from.
➤ Terence Lee
Ruby task force member at Heroku
As a developer of MRI, I get super jealous reading about the JVM ecosystem and
tooling. With this book, Joe has finally made that ecosystem approachable for
JRuby applications.
➤ Zachary Scott
Ruby-core member and maintainer of Sinatra
Deploying with JRuby 9k
Deliver Scalable Web Apps Using the JVM
Joe Kutner
The Pragmatic Bookshelf
Raleigh, North Carolina
Many of the designations used by manufacturers and sellers to distinguish their products
are claimed as trademarks. Where those designations appear in this book, and The Pragmatic
Programmers, LLC was aware of a trademark claim, the designations have been printed in
initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer,
Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade-
marks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes
no responsibility for errors or omissions, or for damages that may result from the use of
information (including program listings) contained herein.
Our Pragmatic books, screencasts, and audio books can help you and your team create
better software and have more fun. Visit us at https://pragprog.com.
The team that produced this book includes:
Brian P. Hogan (editor)
Potomac Indexing, LLC (index)
Linda Recktenwald (copyedit)
Gilson Graphics (layout)
Janet Furlow (producer)
For sales, volume licensing, and support, please contact [email protected].
For international rights, please contact [email protected].
Copyright © 2016 The Pragmatic Programmers, LLC.
All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise,
without the prior consent of the publisher.
Printed in the United States of America.
ISBN-13: 978-1-68050-169-8
Encoded using the finest acid-free high-entropy binary digits.
Book version: P1.0—July 2016
Contents
Acknowledgments . . . . . . . . . . . ix
Preface . . . . . . . . . . . . . . xi
1. Getting Started with JRuby . . . . . . . . . 1
What Makes JRuby So Great? 2
Preparing Your Environment 4
Introducing Warbler 7
Creating a JRuby Microservice 10
Wrapping Up 15
2. Creating a Deployment Environment . . . . . . . 17
Installing Docker 17
Getting Started with Docker 20
Creating a Docker Image 22
Deploying to the Cloud 24
Wrapping Up 27
3. Deploying a Rails Application . . . . . . . . 29
What Is Traditional Deployment? 29
Porting to JRuby 30
Configuring Rails for Production 34
Creating the Deployment Environment 36
Deploying to the Public Cloud 40
Deploying to Private Infrastructure 41
Wrapping Up 48
4. Consuming Backing Services with JRuby . . . . . 49
What Are Backing Services? 49
Storing Sessions in Memcached 50
Running Background Jobs with Sidekiq 56
Contents • vi
Message Passing with RabbitMQ 62
Wrapping Up 71
5. Deploying JRuby in the Enterprise . . . . . . . 73
What Is an Application Server? 74
Getting Started with TorqueBox 75
Scheduling a Recurring Job 77
Using the Cache 78
Deploying to the Public Cloud 81
Deploying to Private Infrastructure 81
Using a Commercially Supported Server 83
Wrapping Up 86
6. Managing a JRuby Application . . . . . . . . 87
Creating a Memory Leak 87
Inspecting the Runtime with VisualVM 88
Inspecting the Runtime with JMX 93
Invoking MBeans Programmatically 96
Creating a Management Bean 98
Using the JRuby Profiler 100
Analyzing a Heap Dump 103
Wrapping Up 107
7. Tuning a JRuby Application . . . . . . . . 109
Setting the Heap Size 109
Setting Metaspace Size 111
Configuring Heap Generations 112
Choosing a Garbage Collector 114
Benchmarking the Garbage Collector 116
Using invokedynamic 120
Wrapping Up 121
8. Monitoring JRuby in Production . . . . . . . 123
Installing the New Relic Gem 123
Creating a New Relic Alert 126
Handling Errors with Rollbar 127
Customizing Rollbar Reporting 131
Wrapping Up 132
9. Using a Continuous Integration Server . . . . . . 133
Installing Jenkins 133
Installing Jenkins Plugins 134
Contents • vii
Creating a Git Depot 135
Creating a Jenkins Job 136
Enabling Continuous Delivery 139
Wrapping Up 140
Index . . . . . . . . . . . . . . 143
Acknowledgments
Writing a book is a lot like writing code. You need to know the rules, recognize
patterns, and occasionally know when to break the rules. Both writing and
coding are crafts. And like with any craft, you improve by getting advice from
more experienced professionals and being critiqued by your peers. I’m so
fortunate to have had this kind of help.
I’m inexpressibly thankful to those who reviewed this book prior to its publi-
cation. I was humbled by the attention to detail and wise feedback they pro-
vided in making it a finished product. Thank you, Jeff Holland, Margaret Le,
Matt Margolis, Jay McGaffigan, Chris Seaton, and Tim Uckun. I consider you
all to be my friends!
I’d also like to thank the staff at the Pragmatic Bookshelf: Susannah Pfalzer,
Dave Thomas, Andy Hunt, and probably a whole bunch of other people I don’t
know about.
Above all, thank you, Brian P. Hogan, my editor. This is our fourth endeavor
together, and as usual I’ve become a better writer because of it. I look forward
to working on future projects together.
I must also thank the creators of the technologies I’ve written about. This
book would not have been possible without your hard work. Thank you,
Charles Nutter, Thomas Enebo, Karol Buček, Christian Meier, Chris Seaton,
and the rest of the JRuby team. You’re the most amazing group in all of the
open source world. I owe you all my deepest gratitude and a free beverage.
Finally, I’d like to thank my wife and son. I could not have completed this
project without your love and support.
report erratum • discuss
Description:Deploy using the JVM's high performance while building your apps in the language you love. This book introduces you to three JRuby deployment strategies that give you the performance and scalability you need, while you use a language designed for rapid development. Explore common deployment scenario