how to get java to capture printing from sybase stored proc

In my experience on Wall St, Sybase store proc can get very complex. A basic technique is the lowly “print”. It beats “select” because under error condition all selects into a log table are rolled back.

Sometimes Sybase print output doesn’t get returned to java. For jdbc I had a simple reusable method to while-loop through a series of warnings. Here’s my technique for spring jdbcTempalte. Note the documented “logging all warnings” may not work. If you don’t override handleWarnings() like I did, then all warnings become exceptions so super.query() return value is lost — real show stopper.

public class GenericProcedureCaller extends JdbcTemplate {
protected void handleWarnings(Statement stmt) {
try {
} catch (SQLWarningException e) {"\t\t v v v output from database server v v v v ");
SQLWarning warn = e.SQLWarning();
while (warn != null) {;
warn = warn.getNextWarning();
}"\t\t ^ ^ ^ output from database server ^ ^ ^ ^ ");
} catch (SQLException e) {
public List query(String sql, RowMapper rowMapper) throws DataAccessException {
boolean oldSetting = isIgnoreWarnings();
// setting to false to capture "prints" from the proc, but there's side effect.
try {
return super.query(sql, rowMapper);
} finally {

factory-method vs in Spring

I feel there’s deeper and richer support for Notes from spring doc —

— “factory bean” vs FactoryBean —
When the Spring documentation makes mention of a ‘factory bean’, this will be a reference to a bean that is configured in the Spring container that will create objects via an instance or static factory method. When the documentation mentions a FactoryBean (notice the capitalization) this is a reference to a Spring-specific FactoryBean.

— getting the FactoryBean object itself —
Finally, there is sometimes a need to ask a container for an actual FactoryBean instance itself, not the bean it produces. This may be achieved by prepending the bean id with ‘&’ when calling the getBean method of the BeanFactory (such as the ApplicationContext bean factory).

So for a given FactoryBean with an id of myBean, invoking getBean(“myBean”) on the container will return the product of the FactoryBean, but invoking getBean(“&myBean”) will return the FactoryBean instance itself.

FactoryBean to avoid hardcoded classnames in spring xml

Without this, you need to hard code the class name in a windows-specific xml, and a different classname in the linux-specific xml

public class CalcLibFactoryBean implements FactoryBean {
public CalcLibWrapper getObject() throws Exception {
if (System.getProperty("").toLowerCase().startsWith("win")) {
return new PxYldCalcWrapperStub();
}else {
return new PxYldCalcWrapper();

convenient spring jdbc methods

1a) Query for map – if your query/proc returns just a single row and you
don’t want to create a new bean class.
* Column names had better be distinct, which is usually easy.
* Similar to boost tuple.
2) Query for object (of your custom class) – if your proc/query returns
a single row, and you already have a bean class to hold all attributes.
3) queryForObject (String sql, String.class) – if you are sure to get
only one row and combine all columns into a single string
4a) Query for int – if your query/proc returns nothing but an integer
4b) Query for object – if your proc/query returns a single Date
5) Row mapper – if your query/proc returns a result set

simplest spring-jmx setup to configure a trading server

Without any annotation or source code change, any public (static or non-static) methods will be accessible on jconsole.


harmless productivity tools ^ frameworks like ..Spring

[[The Productive Programmer]] by Neal Ford advocates learning a large number of _simple_ tools for productivity. Tools could be a complex but we use them in a non-intrusive, simple manner. Many other tools promise to help productivity but are traps – spring, hibernate, jaxb, xslt. Once you employ them they tend to become your master.

I have no such issues with perl/shell scripting, testing tools like junit, unix utilities, text editors, eclipse, maven/ant (?) A critical feature is, the option to use the tool in part of my project, and use alternative tools for the remainder. The tool should not be possessive and exclusive.

Hibernate is more gregarious than Spring, as you can mix Hibernate and straight JDBC in different modules of your system. Spring, being a framework, tends to engulf your entire project. You are then constrained by that framework developers’ decisions.

spring ApplicationContext (MVC++) is like a container or server

AppCx often controls some thread pool(s), esp. in trading systems. AppCx (subclass of BeanFactory) has many “early” features of a container or server —

– It is automatically started at JVM start-up
– it instantiates many objects, calling special methods on each
* It has reference to those objects and therefore serve as a crucial lookup registry or service locator
– it often remains “alive” forever as if a daemon thread

SmartLifeCycle hooks into the start-up. So does InitializationBean and init-method.

Incidentally, there are similar start-up hooks in JMS template.!/org/springframework/jms/core/JmsTemplate.html#execute(org.springframework.jms.core.SessionCallback,%20boolean) says “…allowing the JMS Connection to be started on the fly.” — pure magic. For a topic consumer, this is the crucial but hidden start of subscription. The unsubscribe is equally hidden — when the session is closed.

how a spring app stays alive af main() method returns

In fact, the main() method doesn’t return — prevented from returning. Prevented because main() calls await() of this class below.

protected static class ContextClosedCompleteListener implements ApplicationListener {
private final CountDownLatch countDownLatch = new CountDownLatch(1);
public ContextClosedCompleteListener(ConfigurableApplicationContext applicationContext) {

// required by spring's ApplicationListener
public void onApplicationEvent(ContextClosedCompleteEvent event) {"Received context closed complete event");

// called from main()
public void await() throws InterruptedException {

Spring can add unwanted (unnecessary) complexity

[5] T org.springframework.jms.core.JmsTemplate.execute(SessionCallback action, boolean startConnection) throws JmsException
Execute the action specified by the given action object within a JMS Session. Generalized version of execute(SessionCallback), allowing the JMS Connection to be __started__ on the fly, magically.
Recently i had some difficulties understanding how jms works in my project. ActiveMQ hides some sophisticated stuff behind a simplified “facade”. Spring tries to simplify things further by providing a supposedly elegant and even simpler facade (JmsTemplate etc), so developers don’t need to deal with the JMS api[4]. As usual, spring hides some really sophisticated stuff behind that facade.

Now i have come to the view that such a setup adds to the learning curve rather than shortening it. Quickest learning curve is found in a JMS project using nothing but standard JMS api. This is seldom a good idea overall, but it surely reduces learning curve.

[4] I don’t really know how complicated or dirty it is to use standard JMS api directly!

In order to be proficient and become a problem solver, a new guy joining my team probably need to learn both the spring stuff and the JMS stuff [1]. When things don’t behave as expected[2], perhaps showing unexpected delays and slightly out-of-sync threads, you don’t know if it’s some logic in spring’s implementation, or our spring config, or incorrect usage of JMS or a poor understanding of ActiveMQ. As an analogy, when an alcoholic-myopic-diabetic-cancer patient complains of dizziness, you don’t know the cause.

If you are like me, you would investigate _both_ ActiveMQ and Spring. Then it becomes clear that Spring adds complexity, not reduces complexity. This is perhaps one reason some architects decide to create their own frameworks, so they have full control and don’t need to understand a complex framework created by others.

Here’s another analogy. If a grandpa (like my dad) wants to rely on email everyday, then he must be prepared to “own” a computer with all the complexities. I told my dad a computer is nothing comparable to a cell phone, television, or camera as a fool-proof machine.

[1] for example, how does the broker thread start, at what time, and triggered by what[5]? Which thread runs onMessage(), and at what point during the start-up? When and how are listeners registered? What objects are involved?

[2] even though basic functionality is there and system is usable

non-trivial Spring features in my recent projects

FactoryBean —
lookup-method — method injection using CGLIB
InitializingBean — an alternative to init-method
Auto wiring
@RunWith for unit testing
property place holders
jms messageConverters

– — Simply Magical ! Used to auto start threads when our custom ApplicationContext starts

— @Resource — on a field “cusip” would mean “Mr XMLBeanFactory, please inject into this.cusip an XML bean named cusip”. System implicitly declares this *field* in the xml bean definition of this class, but you need to declare the “cusip” bean.

Q: Just where and how does my aa_reoffer JMS test start the broker daemon thread?
A: Neoreo QueueFactoryBean (FactoryBean derivative) has a getObject(). In it, we create a new JmsTemplate and call execute() on it. This Spring method creates a full-blown jms session, which creates the daemon on demand.

## dependency injection: promises+justifications

I was asked this in 2017, 2019 and 2020! Rare longevity for a jxee topic.

  • “boilerplate code slows you down”…. the wiring
  • separating behavior from configuration. See post on “motivation of spring”
  • wiring logic is extracted from code -> better code density and readability
  • code reuse, compile-time dependency. cut transitive dependency. — u can move the aircon without moving the house. Without DI, “to compile one class, you must compile the entire system”
  • #1 j4: testability
  • “tests are easier to write, so you end up writing more tests.”
  • easy to mock

These are the promises and justifications as experienced users described.

anonymous classes — usage

* Usage: GUI. Event handlers and listeners
* Usage: threads.
* Spring callbacks in templates
(See other posts on sample codes for both.)

anonymous static and non-static classes are routinely used inside static/non-static methods to create a thread and start it.

–> concurrency developers had better get thoroughly familiar with the basic rules of anon classes

proxies in Spring (and java)

Here’s a random list of places we see proxies popping up. I think 80% of these use reflection

* proxy are often used for network-enabled runtimes
** corba
** rmi
** web service
** ejb

* spring remoting
* runtime generated proxies — cglib and jdk
* aop
* TransactionProxyFactoryBean
* method injection

callback (listener,functor,event handler,observer)@what thread@@

Whenever I see a callback, listener, observer or event handler [1], i ask
Q1: In which thread does the callback run?
Q2: In which thread does the caller run? Note caller method might spawn a new thread to run the callback.
Q3: In which thread does the event generator/sender run? Usually not the same as the callback method.
Q4: is there a network daemon running a blocking accept()? I feel this is a fundamental question. In Swing and Case 3 below, there’s no network daemon so event generator can invoke the callback in the same thread.

Case 1: swing — experiments show the callback runs in the event-queue thread i.e. EDT. If you suspend the event-queue thread, you see the thread waits in wait()

Case 2: onMessage
Case 3: spring jdbc template
case 4: servlet event listeners
case 5: java.util.Observer. I think the detailed chapter in [[head first design patterns]] is a single-threaded example. The update operation triggers all the callbacks in a single thread.

I guess in both Swing and Rendezvous, listeners are in a static collection. GC can’t reclaim them. Memory leak! says listeners are strong references and are not garbage collected, so we must remember to remove the listener. Also see

Doug Lea has a chapter on callbacks in a threaded environment.

A worthwhile distinction — Note a “listener” (and sometimes a callback) is an object; a “callback” is often a dynamic method CALL, or you can think of it as a METHOD in source code, loaded into JVM as part of the class object. Now at registration time, the listener’s object reference (ie memory address) is submitted, photo-copied and stored in the registry.

[1] a method, object, nested class..

MVC+dispatcher, in a purist design

See other posts on this blog about spring, struts …

In a purist MVC design, elements below get less and less purist as we move down the list, so I usually spend more time on the first few layers to keep them well-behaving, well-defined and well-understood.

* model — the model in ModelAndView is just a Map, allowing the use of multiple objects keyed by name. In general, “model” means domain model in the form of beans. These classes should not import or depend on controller or view classes. I believe a lot of business logic can go into the models, because it is the behavior of the domain models.

What if some low-level logic is shared between a model and a controller (or something below)? We risk tight coupling if a model imports a class, which in turn imports the controller. Solution: make that utility independent of models or controllers.

* view — should have as little logic as possible. Ideally, views can change in response to controller, so the biz logic lies outside the view — examples include hiding input fields.

View classes need to import model classes, but seldom controller classes. Since controllers control views, it’s a sign of tight coupling if views start to import controller classes. You end up with a entangled architecture — poor separation of concern.

* front dispatcher — usually required. Struts and spring.

* sub-controllers — for each operation or action, such as login, CRUD.

* helper classes but excluding well-defined beans — the miscellaneous. Nothing purist about these. MVC pattern won’t help you much here, but other patterns might.

dependency injection: cost^ROI

Ignore J2EE and EJB for now. Just look at an app with lots of HAS-A relationships. Compared to standardard dependency-setting,

#1 ROI ? testability -> quality assurance

#1 cost? unnatural and perhaps more effort? argues that flexibility is achieved by factory, service locator or DI. All 3 need extra effort compared to the basic, traditional inflexible implementation

dependency injection: %%intro

Inspired by James Shore,

Q: What’s a dependency? What are the 2 parties in a dependency? What’s bad?
A: a “dependency” is one of class A’s non-static fields b, of type B.

“injecting the dependency into A” = assigning an object to the instance var b with a public setter method. This can be done in a third-party class C, through an interface.
“setting the dependency” = A autonomously assigning object to its own instance var b. Common practice

Q: why do people mention factory pattern, service-locator pattern?
A: has a simple example (stop watch) demonstrating constructor injection. also used in dotnet. says

Some call those “dependencies.” Most people call them “instance fields.”


Why Do We Do This?
Among other things, it’s handy for isolating classes during testing.

spring grandiose jargons

service — is basically a CLASS (B) used by your class (A). In Spring, this service becomes an interafce.

service — is often an instance field in your class.

dependency — is basically an instance field. If class A has a B field and A uses some B functionality/service then A functionally depends on B.

^^^^ now we have seen the worst jargon overdose. A regular instance field is now designated a “service” and a “dependency”. And the 2 new terms have different usage contexts. Sometimes you call that field a “dependency”; sometimes you call that same field (or its class) a “service”; but you never call it a “field”

wiring — a serious app need to wire up hundreds of classes — HAS-A. Spring uses an xml config file, lots of interfaces, and an injector for this wiring.

trinity — for dependency injection, you need minimum 3 classes, the service, the client (your app) and the injector. Spring wants a third party to inject the dependency into the client.

In the extreme implementation,
* just about every field is declared an interface type
* every such field has a setter
* you seldom see new ..(). Bean Instantiation happens at appCx start-up time, by “Hollywood”

spring mvc reload,starring wac4ds (AppCx)

wac4ds is the #1 thing to internalize, && the #1 source of initial confusion.

Q: What’s the relationship between a DispatcherSerlvet and a WebApplicationContext (WAC)?
A: “each DispatcherServlet has its own WebApplicationContext” — according to, which even shows a diagram

Q: To drive home the one-to-one mapping, give me the names of a { DS, WAC } pair?
A: These pairs are by no means behind-the-scene unsung heros. golfing-servlet.xml is the WAC if you have


Struts ActionServlet is similar:


In both spring and struts web.xml, this “golfing” name would subsequently be used for url mapping.

Q: is DispatcherServlet ready to use or must be subclassed?
A: No subclass need. You give a customized name to the standard DispatcherServlet in web.xml and use it.

Q: where to put the web application context config file?
A: WEB-INF/your_servlet_name-servlet.xml must be present. known as the
webapplicationContext (wac4ds) for the DispatcherServlet

struts vs spring, component-by-component

1) Struts Actions Are Roughly Spring Controllers

Actions are abstract classes (extends Action@@) and Controllers are interfaces. In order to be configured as a Spring MVC controller, an object would need only to implement the following method:

ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response); # pushmail

the simplest intermediary transition step from Struts to Spring is to rewrite Actions so they implement the Controller interfaces and reuse the existing code. This allows incremental removal of all the Struts dependencies while the application remains operational.

Some of the supplied Controllers match the more specialized Struts Actions with which you may be familiar. For instance, if you use DispatchActions, MultiActionControllers and the more elaborate AbstractWizardFormControllers will be helpful. About a dozen different Controller implementations come with Spring MVC, so it is well worth exploring their purposes and how they can replace your Struts mechanisms.

2) ActionForwards -> ModelAndView

closest component in Spring MVC to ActionForward is the ModelAndView interface. Spring Controllers return implementations of the ModelAndView interface, which, like a Controller, can be custom implemented. Or, if appropriate, you can use the ModelAndView implementations supplied by Spring MVC.

As the name implies, ModelAndView objects have Model and View components. Model components contain the business object to be displayed via the View component. ModelAndView implementations may not have any Model components included. They may simply forward into some form of an actual View component (JSP, XSLT, Tiles, HTML, XML, etc.). As with Controller implementations, I strongly recommend researching Spring MVC-supplied implementations of the Model and View interfaces and View Resolvers.

*) afbean (actionForm beans) have no direct counterpart

*) spring taglig is much smaller than struts 4 taglibs (html, logic, bean ….)

*) ->

*) struts “action path=..” -> simpleUrlMapping to map a request URL to loginController

*) struts “forward name=..” -> viewResolver to translate a logical view name to a URL

*) struts-config.xml -> yourDisplatcher-servlet.xml (WAC4DS) + auxiliary config files

*) execute() -> handleRequest()

struts^spring-mvc: 1-controller@@

Struts also has an “specialized” controller per action — the LoginAction/SignupAction/DetailsAction… classes, even though they only constitute part of the controller. The singular ActionServlet is the other part of the controller.

In summary, For Each action u need 0 or 1 form, 0 or more views, a 2-part controller, 1 mapping to tie them together

As you can see, the Model thingy is elusive.

spring/hibernate transaction AOP interceptor

q 3: which obj intercepts the sql and manages tx propagation, session and thread to enable tx?
A3: “they are intercepted and interpreted by a particular implementation of the org.springframework.transaction.PlatformTransactionManager interface”.

AOP usually requires an interface, as needed by JDK proxy

For example, a hibernate tx mgr is such an interceptor — org.springframework.orm.hibernate.HibernateTransactionManager

Q82: where are “interceptor” and “proxy” mentioned in src?