简单介绍
Java 企业级应用服务器IBM WebSphere 是一款高度可扩展的应用服务器,专为企业级应用程序设计和部署而开发。WebSphere 提供了一个开放式、标准化的平台,支持开发和运行各种 Java EE 应用程序,如 Web 应用程序、企业级 Servlet 和 JSP、EJB 和 JMS 等。
特点
WebSphere 主要特点包括:
-
强大的可扩展性:WebSphere 支持可伸缩性和高可用性,能够满足不同规模和需求的企业应用程序。
-
安全性:WebSphere 提供了一系列安全功能,包括身份验证、授权和加密。
-
高可用性:WebSphere 支持冗余和故障转移,确保应用程序的高可用性和可靠性。
-
管理性:WebSphere 提供了一系列管理工具和 API,使管理员能够轻松地管理和监控应用程序。
-
开放式平台:WebSphere 支持多种标准开发技术和集成标准,如 Java EE、Web 服务、XML 和 JCA 等。
总之,Java 企业级应用服务器IBM WebSphere 是一款功能强大的企业级应用服务器,为企业提供了安全、高可用、可扩展的平台,能够满足不同规模和需求的企业应用程序的需求。
使用场景
IBM WebSphere是一个企业级的应用服务器,主要用于开发、集成和部署企业级软件应用程序。它提供了各种功能和工具,帮助企业建立稳定的、可靠的、高性能的应用程序,包括以下方面:
-
Web应用程序开发:WebSphere提供了强大的工具和API,帮助开发人员构建高度可靠的、安全的Web应用程序,这些Web应用程序可以支持大量的并发用户,访问量大。
-
事务处理:企业应用程序通常需要管理复杂的事务过程,例如银行交易、订单处理等。WebSphere具有优秀的事务管理功能,能够确保应用程序执行的一致性和完整性。
-
数据库集成:大多数企业的应用程序需要使用数据库来存储和管理数据,WebSphere可以与各种数据库集成,例如Oracle、SQL Server等。
-
消息传递:企业应用程序通常需要使用消息传递机制完成异步通信,例如JMS消息服务。WebSphere提供了完整的消息传递支持,使得应用程序能够轻松地完成消息交换。
-
安全性:WebSphere提供了丰富的安全功能,包括身份验证、授权、加密等,确保企业应用程序的安全性能。
面试题
问题:
-
IBM WebSphere的使用场景是什么?
-
WebSphere的主要功能是什么?
-
如何使用WebSphere开发企业级应用程序?
-
如何将WebSphere集成到企业级应用程序中?
-
如何提高WebSphere的性能和可靠性?
解答:
1. 什么是WebSphere?
IBM WebSphere是一种基于Java的应用程序服务器,可提供企业级应用程序的发展和执行环境。该服务器支持多种应用程序和Web服务标准,包括Java EE,Web 2.0和SOA(面向服务的架构)应用程序。
2. 如何启动/停止WebSphere服务器?
启动服务器:
在Windows操作系统中,您可以在单击适当的启动命令之后通过启动该服务器控制台来启动WebSphere服务器。在Unix或Linux系统上,您可以使用以下命令启动该服务器:
<wabSphere-install-root>/bin/startServer.sh server1
停止服务器:
在Windows操作系统中,您可以在单击适当的停止命令之后通过停止该服务器控制台来停止WebSphere服务器。在Unix或Linux操作系统上,您可以使用以下命令停止服务器:
<wabSphere-install-root>/bin/stopServer.sh server1
3. 如何配置WebSphere数据源?
配置数据源时,需要执行以下步骤:
- 在WebSphere管理控制台中选择JDBC提供程序。
- 按照提供程序的提示操作来配置数据源。您需要为数据源指定一个JNDI名称,这将与应用程序代码中的JNDI名称对应。
- 在WebSphere服务器控制台的数据源中对数据源进行测试,以确保数据源正确连接。
4. 如何在WebSphere中管理会话?
WebSphere支持各种会话管理选项,包括使用COOKIE, URL,本地会话存储和分布式会话存储等。您可以使用以下代码示例将用户会话存储在WebSphere本地存储中:
HttpSession session = request.getSession(true);
// Set session attribute value
session.setAttribute("username", "John");
5. 什么是WebSphere插件?
WebSphere插件是一种与IBM HTTP服务器集成的模块,可将请求转发到WebSphere服务器。插件可以通过WebSphere管理控制台进行配置,以处理特定类型的请求。
6. 如何在WebSphere中配置集群?
要在WebSphere中配置集群,需要执行以下步骤:
- 在WebSphere管理控制台中创建新的服务器集群。
- 按照提供程序的提示操作来配置集群。
- 配置负载平衡器,以便将请求路由到集群中的每个服务器。
- 对集群进行测试,以确保所有服务器均可接受请求。
7. 如何在WebSphere中配置JMS?
在WebSphere中配置Java消息服务(JMS)时,需要执行以下步骤:
- 在J2EE资源> JMS资源> JMS提供程序中创建新的JMS提供程序。
- 按照提供程序的提示操作来配置JMS提供程序。
- 创建JMS目标,如JMS队列或主题,您可以使用Java EE应用程序中的JNDI名称引用它们。
- 在应用程序中使用JMS API连接到JMS提供程序,并发送/接收消息。
8. 如何使用WebSphere部署应用程序?
要在WebSphere中部署应用程序,需要执行以下步骤:
- 编译并打包应用程序,以创建一个WAR或EAR文件。
- 在WebSphere管理控制台中,选择应用程序> 新建并创建新的应用程序。
- 选择您的应用程序文件(WAR或EAR),并按照提供程序的提示操作来部署应用程序。
- 对应用程序进行测试,以确保它已正确部署并可以正常工作。
9. 如何在WebSphere中管理线程池?
您可以使用以下代码示例来管理WebSphere线程池:
// get thread pool
ThreadPool pool = MessageBroker.getInstance().getService().getThreadPool();
// set min and max threads
pool.setCoreThreads(10);
pool.setMaxThreads(100);
// set queue depth
pool.setQueueDepth(1000);
10. 如何使用WebSphere提供程序执行事务?
您可以使用WebSphere提供程序执行Java事务,使用以下代码示例:
try {
// Get UserTransaction object
UserTransaction ut = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");
// Begin transaction
ut.begin();
// Perform transactional task(s)
// ...
// Commit transaction
ut.commit();
} catch (Exception ex) {
// Handle exception and
// rollback transaction if necessary
}
总结: 以上是几个常见的WebSphere面试题,在面试前可以提前准备并深入理解这些问题,以便更好地在面试中表现自己。除了上面提到的这些问题外,面试中可能还会涉及到其他技术方面的问题,因此建议面试前进行全面准备。
安装教程
在Win上安装IBM WebSphere
-
下载安装包并解压:从IBM官网下载WebSphere安装包,解压到本地目录。
-
打开安装向导:双击解压后的安装程序,在弹出的用户许可协议页面上阅读并接受协议。
-
选择安装类型:在下一步中,选择“Typical”或“Custom”,前者是安装WebSphere的标准设置,后者是用户自定义安装。
-
指定安装目录:设定WebSphere应用服务器的安装路径,一般在C盘的Program Files目录下。
-
指定服务账户:将服务账户和密码设定在下一步中,并设置WebSphere的JVM参数。
-
安装完成:安装完成后,启动WebSphere应用服务器。
在Linux上安装IBM WebSphere
-
下载安装包并解压:从IBM官网下载WebSphere安装包,解压到本地目录,并给出相应的执行权。
-
开始安装:打开终端,运行安装程序。
-
阅读许可协议:在安装向导中,阅读并接受许可协议。
-
选择安装类型:在下一步中选择“Typical”或“Custom”。
-
指定安装目录:在指定安装目录时,选择可用的目录及磁盘空间。
-
设置JVM参数:使用默认的JVM参数或设置自己的参数。
-
选择Web服务器类型:选择或使用默认的Web服务器类型。
-
安装完成:安装完成后,选择启动WebSphere应用服务器。
命令:
Win上安装IBM WebSphere的步骤可通过图形界面实现,不需要使用任何命令。
Linux上安装IBM WebSphere的命令:
-
解压WebSphere安装包:tar zxvf ibm_was_package.tar.gz
-
运行安装程序:./install
-
启动WebSphere应用服务器:/opt/IBM/WebSphere/AppServer/bin/startServer.sh server1
常见问题
1. 问题:应用服务器启动时,出现类似 "Error 404: javax.servlet.ServletException: SRVE0207E" 的错误。
原因:该错误通常是由于 WebSphere 没有正确配置 Web 应用程序引起的。
解决办法:
1) 确保应用程序已经部署到正确的上下文根路径下。
2) 确保应用程序的 WAR 文件已经被正确部署。可以在管理控制台中查看应用程序是否处于运行状态。
3) 确保应用程序的部署描述符(web.xml)文件正确配置。可以使用以下示例来检查是否有任何问题:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>YourWebApp</display-name>
<!-- 配置 servlet -->
<servlet>
<servlet-name>YourServlet</servlet-name>
<servlet-class>com.yourcompany.YourServlet</servlet-class>
</servlet>
<!-- 配置 servlet 映射 -->
<servlet-mapping>
<servlet-name>YourServlet</servlet-name>
<url-pattern>/yourUrlPattern</url-pattern>
</servlet-mapping>
</web-app>
2. 问题:WebSphere 访问应用程序时,出现类似 "ClassNotFoundException: com.yourcompany.YourClass" 的错误。
原因:该错误通常是由于 WebSphere 没有正确配置应用程序的类路径引起的。
解决办法:
1) 检查应用程序的 MANIFEST.MF 文件,确保所有必需的库文件都被添加到 Class-Path 属性中。这些库文件应该位于应用程序的 lib 目录中。
例如:
Class-Path: lib/library1.jar lib/library2.jar
2) 检查应用程序的类路径是否正确设置。在管理控制台中,单击“应用程序” - “选择应用程序” - “类加载器”来查看类路径。
例如:
usr/local/YourWebApp/WEB-INF/classes/
usr/local/YourWebApp/WEB-INF/lib/
3) 确保所有必需的库文件都位于正确的路径中。可以在应用程序的 web.xml 文件中添加以下代码片段来检查是否有任何问题:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
3. 问题:应用程序启动时,出现类似 "java.lang.OutOfMemoryError" 的错误。
原因:该错误通常是由于应用程序分配的内存超过了可用内存限制引起的。
解决办法:
1) 增加 JVM 的最大堆大小。可以将以下代码段添加到 WebSphere 启动脚本中:
-Xmx1024m
该代码段将最大堆大小增加到 1024 MB。
2) 优化应用程序以减少内存使用量。例如,可以减少缓存的大小或限制同时处理的请求数量。
下面是一个示例,可以在应用程序的 web.xml 文件中配置最大请求数量:
<context-param>
<param-name>maxRequestThreads</param-name>
<param-value>100</param-value>
</context-param>
整合使用
步骤:
- 确定WebSphere的安装路径,并将WebSphere的lib目录下的所有jar文件添加到项目的classpath中。
- 在pom.xml文件中添加WebSphere的依赖项:
<dependency> <groupId>com.ibm.websphere</groupId> <artifactId>com.ibm.ws.ejb.thinclient</artifactId> <version>${websphere.version}</version> </dependency>
- 在application.properties中配置WebSphere的连接信息:
# WebSphere Connection Details websphere.host=127.0.0.1 websphere.port=9080 websphere.username=wasadmin websphere.password=wasadmin websphere.jndiName=ejb/mysessionbean
- 创建一个接口来定义标准的业务方法:
public interface MySessionBeanRemote { void sayHello(String message); }
-
创建一个类来实现MySessionBeanRemote接口:
@Stateless public class MySessionBean implements MySessionBeanRemote { public void sayHello(String message) { System.out.println("Hello " + message); } }
-
创建一个类来管理WebSphere连接,并注入MySessionBeanRemote:
@Component public class WebSphereEJBClient { private Context ic; private MySessionBeanRemote mySessionBean; @Value("${websphere.jndiName}") private String jndiName; public WebSphereEJBClient() throws NamingException { Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"); env.put(Context.PROVIDER_URL, "corbaloc:iiop:" + System.getProperty("websphere.host") + ":" + System.getProperty("websphere.port")); try { ic = new InitialContext(env); } catch (NamingException e) { System.out.println("Could not create JNDI API context: " + e.getMessage()); } Object obj = ic.lookup(jndiName); mySessionBean = (MySessionBeanRemote) PortableRemoteObject.narrow(obj, MySessionBeanRemote.class); } public void callMySessionBean(String message) { mySessionBean.sayHello(message); } }
-
在Spring Boot应用程序中注入WebSphereEJBClient,并使用它来调用MySessionBeanRemote的方法:
@RestController @SpringBootApplication public class Application implements CommandLineRunner { @Autowired private WebSphereEJBClient webSphereEJBClient; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... strings) throws Exception { webSphereEJBClient.callMySessionBean("Spring Boot"); // Call MySessionBeanRemote method } }
完整代码:
-
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- WebSphere dependencies --> <dependency> <groupId>com.ibm.websphere</groupId> <artifactId>com.ibm.ws.ejb.thinclient</artifactId> <version>8.5.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
- application.properties
# WebSphere Connection Details websphere.host=127.0.0.1 websphere.port=9080 websphere.username=wasadmin websphere.password=wasadmin websphere.jndiName=ejb/mysessionbean
- MySessionBeanRemote.java
public interface MySessionBeanRemote { void sayHello(String message); }
-
MySessionBean.java
@Stateless public class MySessionBean implements MySessionBeanRemote { public void sayHello(String message) { System.out.println("Hello " + message); } }
-
WebSphereEJBClient.java
@Component public class WebSphereEJBClient { private Context ic; private MySessionBeanRemote mySessionBean; @Value("${websphere.jndiName}") private String jndiName; public WebSphereEJBClient() throws NamingException { Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"); env.put(Context.PROVIDER_URL, "corbaloc:iiop:" + System.getProperty("websphere.host") + ":" + System.getProperty("websphere.port")); try { ic = new InitialContext(env); } catch (NamingException e) { System.out.println("Could not create JNDI API context: " + e.getMessage()); } Object obj = ic.lookup(jndiName); mySessionBean = (MySessionBeanRemote) PortableRemoteObject.narrow(obj, MySessionBeanRemote.class); } public void callMySessionBean(String message) { mySessionBean.sayHello(message); } }
-
Application.java
@RestController @SpringBootApplication public class Application implements CommandLineRunner { @Autowired private WebSphereEJBClient webSphereEJBClient; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... strings) throws Exception { webSphereEJBClient.callMySessionBean("Spring Boot"); // Call MySessionBeanRemote method } }
文章评论